
đ„Blog # 25: From Red to Green: Taming Unreliable Tests with Playwrightâs Retry Magic
Published on May 20, 2025
Hey folks đ If youâve ever shouted "But it worked yesterday!" at your test suite â welcome to the club.
In todayâs post, Iâm sharing my real-world experience dealing with flaky tests in Playwright and how I used retry logic to keep my sanity intact (well, mostly!).
Sometimes it failed due to timeouts. Sometimes due to delayed elements. Sometimes the page just didnât load fast enough.
Thatâs when I learned the infamous name for this phenomenon: Flaky Tests.đ
đ§ Retry via playwright.config.js
đĄ You can even set retries differently for CI vs local runs:
In todayâs post, Iâm sharing my real-world experience dealing with flaky tests in Playwright and how I used retry logic to keep my sanity intact (well, mostly!).
đ§Ș The Problem: Tests That Fail Randomly (A Testerâs Nightmare)
Picture this: I wrote a test that checked for a simple piece of text on a page. It ran smoothly on my machineâuntil suddenly, it didnât. đ€Sometimes it failed due to timeouts. Sometimes due to delayed elements. Sometimes the page just didnât load fast enough.
Thatâs when I learned the infamous name for this phenomenon: Flaky Tests.đ
đ Enter Retries â Playwrightâs Built-In Magic
Playwright offers a super handy way to automatically rerun failed tests. You can set this up in the config file or directly via CLI.đ§ Retry via playwright.config.js
// playwright.config.js
export default {
retries: 2, // Retry failed tests up to 2 times
use: { headless: true }
};
Boom! No more random failures (or at least, fewer).đĄ You can even set retries differently for CI vs local runs:
export default {
retries: process.env.CI ? 2 : 0,
};
đ§« A Real-World Example: Dynamic Content Loading
Letâs simulate a flaky scenario using this awesome site:đ https://the-internet.herokuapp.com/dynamic_loading/1
Hereâs a Playwright test that sometimes fails due to slow loading:
const { test, expect } = require('@playwright/test');
test('flaky test with dynamic loading', async ({ page }) => {
await page.goto('https://the-internet.herokuapp.com/dynamic_loading/1');
await page.click('button'); // Start loading content
await expect(page.locator('#finish')).toHaveText('Hello World!');
});
If âHello World!â doesnât load fast enough, the test may fail. But with retries: 2, Playwright gives it a couple of extra chances.đ CLI Retry for Quick Debugging
Donât want to touch your config file? No problem. You can use the CLI:npx playwright test --retries=3
This saved me during debugging sessions when I wanted fast feedback without committing config changes.đ§ Pro Tip: Retry Only Whatâs Needed
You donât always want to retry every test. Sometimes only one test is flaky.Use this selectivelyâespecially when the flakiness is temporary and known.test.describe.configure({ retries: 1 });
test('occasionally flaky API test', async ({ page }) => {
// this one can use a retry
});
â Mistakes I Made (So You Donât Have To)
While exploring retries, I ran into a few bumps that taught me some important lessons:
- Retrying too many times hides real problems
- Retries donât replace root cause analysis
- Be careful with data-changing tests
đ My Retry Cheat Sheet
Scenario HowRetry all tests retries: 2 in playwright.config.js
Retry a test group test.describe.configure({ retries: 1 })
CLI retries npx playwright test --retries=3
Custom retry logic for-loop with try/catch and delays
đ Final Thoughts
Retries are a great toolâbut not a permanent fix. They helped me gain control over flaky tests while I worked on better test design and smarter waiting strategies.Remember:
- Retry the right tests
- Use retry info (test.info().retry) to debug
- And most importantlyâfix the root cause when you can!
Well, thatâs a wrap on retries! I hope this post helps you feel a little more in control when your tests act up. If youâve got retry tips or horror stories of your own, Iâd love to hear themâletâs learn together!
Until next time, happy testing and fewer flakes! âš