How to Fix Flaky Tests: Causes, Diagnosis and Cures
A flaky test passes and fails on the same code. One flaky test is an annoyance; ten of them and the team starts ignoring red builds, which defeats the purpose of automation. Fixing flakiness is one of the most valuable things an automation engineer can do.
Step 1: Prove it's flaky
Before fixing anything, reproduce it:
# Playwright: run one test many times
npx playwright test cart.spec.ts --repeat-each=50 --workers=4
Note the failure rate and collect artefacts from failures: traces, screenshots, videos, logs. A test that fails 1 in 50 runs needs a different investigation from one that fails 1 in 3.
Step 2: Find the cause
1. Timing (the most common)
Symptoms: "element not found", "element not clickable", assertions on stale values. It fails more on slow CI machines.
Causes: hard waits that are sometimes too short; asserting before data has loaded; clicking during an animation or re-render.
Fixes:
Replace sleep() with retrying assertions (await expect(locator).toHaveText(...))
Wait for the condition you need, like a network response or a specific state, not an arbitrary time
Symptoms: passes alone, fails in the full suite or in parallel.
Causes: tests share an account, a cart or a database row; one test's leftovers break another.
Fixes:
Each test creates its own data (unique emails, fresh carts)
Use isolated browser contexts and separate accounts per worker
Clean up, or better, design tests that don't care about leftovers
3. Order dependence
Symptoms: fails when run alone or in a different order.
Fix: no test should rely on another having run. Randomise the order occasionally to expose hidden dependencies.
4. Environment instability
Symptoms: many unrelated tests fail at once; timeouts on third-party calls.
Causes: an overloaded test environment, a slow shared database, real third-party services (payments, email), deploys during test runs.
Fixes:
Mock or stub third-party services in most tests; keep a few contract tests against the real ones
Give CI its own environment, or at least its own data
Health-check the environment before the suite starts
5. Brittle locators
Symptoms: breaks after unrelated UI changes, or matches the wrong element sometimes.
Fixes: use role, label or test-ID locators, and make locators unique. See our locator strategy guide.
6. Real bugs that are intermittent
Sometimes the test is fine and the app is flaky: race conditions, double-submit bugs, caching. Before "fixing" the test, ask whether a user could hit the same failure. Intermittent failures around checkout or booking are often real defects.
Step 3: Manage flakiness as a team
Quarantine: move known-flaky tests to a separate, non-blocking job with a ticket and an owner
Measure: track the flake rate per test; fix the worst ones first
Use retries carefully: one retry to collect a trace is fine, but retries that hide flakiness permanently are not
Set a rule: a new test must pass many repeated runs before it joins the blocking suite
A quick diagnosis table
Symptom
Likely cause
Fails on CI, passes locally
Timing, environment speed, missing data
Fails only in parallel
Shared accounts or data
Fails only when run alone
Order dependence
Many tests fail together
Environment or third-party outage
Fails after UI tweaks
Brittle locators
Same flow fails for real users too
An actual race condition in the app
Build better instincts
Flaky tests often reveal real timing bugs like double submits and stale totals. Practise spotting those by hand in QAUnlocked Practice Labs, where the store lab hides a duplicate-order defect that only appears when you submit twice, then think about how a stable automated test would catch it.
An honest comparison of the three most popular browser automation tools, covering architecture, language support, browsers, speed, debugging and ecosystem, and which to pick for your situation.
Most broken UI tests break on locators. A practical priority order for locators (role, label, text, test ID, CSS), with examples in Playwright and Cypress and the anti-patterns to avoid.
Install Playwright, write and run your first end-to-end test, use role-based locators and web-first assertions, debug with traces, and run it in CI, with tips to keep it reliable from day one.
Be the first to comment.