How to Fix Flaky Tests in Playwright: 10 Battle-Tested Strategies
Published: ·5 min read
Flaky tests destroy team confidence and slow deployment. Here are 10 proven strategies to eliminate flaky Playwright tests — from someone who's fixed thousands of them.
The Cost of Flaky Tests
A flaky test is one that sometimes passes and sometimes fails without any code changes. They seem harmless at first — just re-run the pipeline, right?
Wrong. Flaky tests are silent killers:
Eroded trust — Teams stop believing test results
Wasted time — Engineers debug tests instead of building features
Slower releases — "Just re-run it" becomes the norm
Hidden bugs — Real failures get dismissed as "flaky"
At one company, I inherited a test suite with a 68% pass rate. Not because the application was broken — but because the tests were. Here's how I fixed it.
Strategy 1: Use Role-Based Locators
The problem: CSS selectors and XPath break when developers change class names or restructure HTML.
The problem: Clicking a button that's still loading, or reading text before it's rendered.
The fix: Wait for loading indicators to disappear:
// Wait for spinner to go away
await expect(page.getByTestId('loading-spinner')).toBeHidden();
// Then interact with the element
await page.getByRole('button', { name: 'Submit' }).click();
Or wait for the element to be in a specific state:
Don't make them too long — slow failures are frustrating. Find the right balance for your infrastructure.
Bonus: The Flaky Test Triage Process
When you encounter a flaky test, follow this process:
1. Reproduce locally
Run the test 10 times:
npx playwright test tests/checkout.spec.ts --repeat-each=10
If it passes every time locally but fails in CI, it's likely a timing or environment issue.
2. Check the trace
Open the trace file from CI and look for:
Slow network requests
Elements not visible when clicked
Unexpected modal or popup
3. Identify the root cause
Common causes:
Hard-coded wait times
Race conditions
Shared test state
Unstable locators
4. Fix or delete
If you can't fix it after 30 minutes, delete it. A flaky test is worse than no test. You can always rewrite it properly later.
My Flaky Test Scorecard
After applying these strategies at CooperVision, we went from 68% pass rate to 98%+ in three months.
Pass rate:
Before: 68%
After: 98.5%
Average test time:
Before: 4.2 minutes
After: 1.8 minutes
"Re-run pipeline" requests:
Before: Daily
After: Rare
Team trust in automation:
Before: Low
After: High
The biggest win wasn't technical — it was cultural. When tests are reliable, developers actually care about failures.
Need Help with Your Flaky Tests?
If your test suite is unreliable and slowing down your team, I can help. I've fixed test suites at Fortune 500 companies and can usually identify the main issues in just a few hours.
Native Drag-and-Drop Automation Arrives in Playwright MCP: What v0.0.71 Changes
Playwright MCP v0.0.71 ships browser_drop. It gives you native drag-and-drop from any MCP client. No more evaluate scripts. No more mouse.move chains. Grid reordering, file drop zones, text editor drags — all work the same way a real user does.