Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

To fix a flaky test, confirm it is actually flaky by rerunning it several times, reproduce it in isolation, and capture logs, screenshots, video, and traces from the failures. Then identify the root cause, usually timing and waits, shared state, test data, the environment, or an external dependency, and apply the matching fix such as smart waits instead of sleeps, stable selectors, mocked dependencies, and isolated state. Verify the fix holds across repeated runs, and quarantine plus track any test you cannot fix immediately so it never silently blocks your pipeline.
This guide is about repairing tests that are already flaky. To stop new ones from appearing in the first place, see How to Avoid Flaky Tests?
Fixing flakiness is a repeatable loop, not a guess. Work through these steps in order:
A flaky test is one that produces different results, passing on one run and failing on the next, against the exact same code and inputs. The outcome is non-deterministic, so a red build no longer reliably means a real defect. This erodes trust fast: industry surveys report that a majority of developers deal with flaky tests on a monthly, weekly, or even daily basis, and Google's testing research found that a large share of pass-to-fail transitions in their continuous integration were flaky failures rather than true regressions. Once teams stop trusting the suite, they start ignoring failures, and that is when real bugs slip through.
Before you change anything, prove the test is non-deterministic rather than consistently broken. Run it repeatedly and observe whether the result flips:
A test that flakes only inside the full suite is usually being affected by something outside itself. Narrow it down:
If the failure only shows up in the browser layer, step through it with the techniques in How Do I Troubleshoot and Debug Selenium Tests?
Flaky failures are hard to fix precisely because they are rare, so capture everything the moment they happen:
Running tests on a cloud grid such as TestMu AI Selenium Automation helps here, because each session automatically records video, console, and network logs, giving you a captured artifact for every flaky run instead of an unreproducible one-off.
Almost all flakiness falls into a handful of categories. Match the evidence from Step 3 to one of these root causes, then jump to the matching fix in Step 5.
| Root cause | What it looks like | Matching fix |
|---|---|---|
| Timing / async waits | Element-not-found or assertion fails when the app is slightly slow. | Explicit waits on a condition; never fixed sleeps. |
| Race / concurrency | Fails only under parallel runs or load. | Remove shared mutable state; synchronize access. |
| Order dependency / shared state | Passes alone, fails inside the suite or in a new order. | Reset state before each test; full isolation. |
| Environment instability | Fails on one machine, agent, or browser version only. | Clean, consistent, isolated environments. |
| External / third-party services | Fails when an API or dependency is slow or down. | Stub or mock the dependency. |
| Non-determinism | Fails around date, time, locale, or random values. | Seed the RNG; mock the system clock; pin locale. |
| Brittle selectors | Breaks when markup or layout shifts slightly. | Stable, unique locators such as data-testid. |
This is where the test actually gets repaired. Apply the fix that matches the cause you diagnosed:
When the flakiness traces back to test data living in spreadsheets or external files, externalizing and stabilizing that data, as covered in What Is Apache POI in Selenium?, removes another common source of non-determinism.
A single green run does not prove a flaky test is fixed, the original failure was rare to begin with. Re-run the repaired test under the same pressure that exposed it:
Sometimes you cannot root-cause a flaky test immediately, but you also cannot let it block every build. Quarantine it responsibly:
Manually rerunning tests works for one flaky case, but across thousands of tests and builds you need data. TestMu AI mines historical run data to flag tests that flip non-deterministically, ranks them by severity based on flake rate, and groups failures by likely root cause, so you can target the worst offenders first instead of triaging blindly. You can read more in the Analytics Modules Test Intelligence Flaky Test Analytics documentation.
The cloud grid also makes reproduction far easier. Running the suspect test in parallel across real browsers and operating systems reproduces environment and timing flakiness quickly, and every session ships with the video, console, and network artifacts you need for Step 3, turning a "can't reproduce" failure into one you can actually debug.
Repairing a flaky test is only half the job, the same patterns will reappear if your tests are not designed for stability. Bake in stable selectors, explicit waits, isolation, and mocked dependencies from the start so new flakiness never gets a foothold. For the full proactive playbook, see How to Avoid Flaky Tests?
Most flakiness comes from timing and async waits, race conditions, test order dependency and shared state, unstable environments, third-party or external service dependencies, non-determinism such as system time and random values, and brittle element selectors. Diagnosing which one applies is the key to a lasting fix.
Rerun the test many times under identical conditions and watch for inconsistent results, then track pass and fail history across builds in CI. Test analytics that mine historical run data can surface tests that flip non-deterministically across builds and rank them by flake rate so you do not have to find them by hand.
No. Retries hide flakiness, they do not fix it. Use minimal, temporary retries only as mitigation to keep the pipeline moving while you root-cause and repair the test. A test that only passes on a retry is still broken and still erodes trust in your suite.
Quarantining isolates a flaky test so it still runs and reports its result but does not block the pipeline or fail the build. You tag it, track it as test debt with an owner, and remove it from quarantine once it is fixed and verified, so it is a holding pattern, not a place to forget tests forever.
Typically 20 to 50 runs, ideally both locally and in CI where conditions differ. If it fails inconsistently across those runs it is flaky. If it never reproduces, it is likely not flaky, or too rare to chase right now, so log the conditions and deprioritize rather than burning time on it.
Fixing is reactive: you repair tests that already flake by diagnosing and correcting the root cause. Avoiding is proactive: you design tests so they never flake, using stable selectors, explicit waits, isolation, and mocked dependencies from the start. See How to Avoid Flaky Tests? for the prevention side.
KaneAI - Testing Assistant
World’s first AI-Native E2E testing agent.

TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance