A flaky test passes and fails on the same code with nothing changed. In UI tests the most common cause is timing: the test checks the page before the page is ready. We ran the same check 80 times in three versions on a page that loads in a random 0 to 3 seconds: a 1-second hard wait failed 51 of 80 runs (64%), a 2-second web-first assertion failed 23 (29%), and a 5-second web-first assertion failed none. Two retries hid most failures but not all, and made the suite 39 to 50% slower.
Table of Contents
Table of Contents
Every test team has a test that is “just flaky”. It goes red, someone clicks re-run, it goes green, and everyone moves on. We wanted real numbers instead of folklore, so we built a switch into our practice shop that makes one page load at a random speed, then ran one simple check hundreds of times in three different ways.
Want to run the same checks on Chrome, Firefox and WebKit in CI for free? We compared that route with paid clouds in BrowserStack alternatives.
What is a flaky test?
A test is flaky when its result changes between runs while the code, the test and the data stay the same. That makes it different from two related problems:
- A failing test fails every time. It usually points to a real bug, or a test that is simply wrong.
- A brittle test breaks after a small, harmless change in the app, such as a renamed button. We measured that separately in our Playwright locators test, where 5 of 9 locators broke after one rename.
- A flaky test changes result with no change at all. Playwright’s own report uses the same word: a test that “failed on the first run, but passed when retried” is labelled flaky.
Flaky tests cost trust. Once a team learns that red sometimes means nothing, it starts ignoring red, and then real failures get re-run until they pass too. For a one-paragraph definition, see the flaky test glossary entry.
How did we test flaky tests?
- Target: the products page of our free AITG Demo Shop (v3.0.0). Its
?flaky=1switch makes the product list appear after a random 0 to 3 seconds. Without the switch it appears after a fixed 0.3 seconds. - Tool: Playwright 1.63.0, Chromium, headless, 4 workers, in a cloud container.
- The check: “12 products are on the page”, written three ways: A waits a fixed 1 second, then counts (a hard wait); B uses a web-first assertion that keeps checking for up to 2 seconds; C uses the same assertion with Playwright’s default 5 seconds.
- Runs: each check 40 times (
--repeat-each=40) in three setups: switch off, switch on, and switch on with 2 retries. We ran the whole thing twice on 30 November 2026 (around 12:45 and 23:40 Pakistan time), so every “switch on” number below is out of 80.
What did we find?
| Setup | A: hard wait 1 s | B: wait up to 2 s | C: wait up to 5 s |
|---|---|---|---|
| Normal page (fixed 0.3 s load) | 80 of 80 passed | 80 of 80 passed | 80 of 80 passed |
| Flaky page, no retries | 29 of 80 passed (64% failed) | 57 of 80 passed (29% failed) | 80 of 80 passed |
| Flaky page, 2 retries | 61 of 80 passed in the end (26 only on a retry); 19 failed all 3 tries | 79 of 80 in the end (28 only on a retry); 1 failed all 3 | 80 of 80, no retries needed |
Suite time with the switch on went from 66.8 to 92.6 seconds in the first run and from 64.7 to 96.9 seconds in the second when we turned on 2 retries: 39% and 50% slower. Retries also meant more browser runs: in the second run test A needed 85 attempts instead of 40.

1. On a normal page, all three versions look equally good
With a steady 0.3-second load, the hard wait passed 80 of 80 times. The problem was in the test all along; it only showed when timing varied. That is why flaky tests seem to appear “suddenly” on a slower CI machine or a busy day.
2. Waiting for a condition beats waiting for a time
Version C waited for what it needed (12 products) and never failed. Version B used the right method but a limit shorter than the slowest real load, so it failed less often than A (29% instead of 64%). That is not safer, it is just harder to notice. Playwright’s docs say “by default, the timeout for assertions is set to 5 seconds”, and here the default was long enough because our worst case was 3 seconds.
3. Retries are a painkiller, not a cure
With 2 retries, the hard-wait test still failed all three tries 19 times out of 80, and the suite got 39 to 50% slower. The useful part of retries is the label: 54 tests passed only on a retry, and Playwright marked each one “flaky” in the report. That list is your fix list.

What causes flaky tests?
We measured the first cause only. The others are common in real suites; we list them so you know where to look.
- Timing and waits (tested above): the test acts or checks before the page, the API or an animation is ready.
- Test order and shared state: one test leaves data or a login behind that another test depends on, so results change with the order or the number of workers. Not tested here.
- Changing test data: dates, time zones, random ids, accounts that other tests also use. Not tested here.
- Layout shifts and late elements: a banner pushes a button down just before the click. Our shop has a
?shift=1switch for this; not part of this run. - External services and network: third-party APIs, rate limits, slow CDNs.
- Concurrency: parallel workers sharing one account or one database row.
- Environment: CI machines that are slower or smaller than laptops, other browsers or screen sizes.
How do you fix flaky tests?
- Find them. Run a suspicious test many times:
npx playwright test --repeat-each=40. In CI, read the “flaky” label in the HTML or JSON report. - Replace fixed waits with waits for a condition. Use web-first assertions such as
toHaveCount,toBeVisibleortoHaveTextinstead ofwaitForTimeoutfollowed by a one-time check. - Set timeouts from the real worst case, measured on CI, not on a fast laptop. Our B version shows what happens when the limit is too short.
- Give each test its own data and reset state before it starts. Playwright’s docs on retries say it plainly: it is “usually better to make your tests isolated”.
- Use stable locators (role, label, test id) so a timing fix is not undone by the next UI change.
- Quarantine, don’t ignore. Move a known flaky test to a separate job with an owner and a date to fix it.
- Use retries only with the flaky report on, and track the count of flaky tests over time. A growing number is a warning.
Can you try this yourself?
Yes. Open the practice shop with the switch on: demo.aitestingguide.com/shop/index.html?flaky=1, and reload a few times to see the load time change. These are the three tests we ran:
const { test, expect } = require('@playwright/test');
const FLAKY = process.env.FLAKY === '1' ? '?flaky=1' : '?flaky=0';
test.beforeEach(async ({ page }) => { await page.goto('/shop/index.html' + FLAKY); });
test('A hard wait 1s then count', async ({ page }) => {
await page.waitForTimeout(1000);
expect(await page.getByTestId('product-card').count()).toBe(12);
});
test('B web-first assertion, 2s timeout', async ({ page }) => {
await expect(page.getByTestId('product-card')).toHaveCount(12, { timeout: 2000 });
});
test('C web-first assertion, default 5s timeout', async ({ page }) => {
await expect(page.getByTestId('product-card')).toHaveCount(12);
});Set baseURL to https://demo.aitestingguide.com in your config, then run FLAKY=1 npx playwright test --repeat-each=40, and once more with --retries=2. Your percentages will differ a little from ours; the pattern should not.
Do AI testing tools fix flaky tests?
We have not run this flaky switch through an AI tool yet, so we make no claim here. One related data point from our regression testing tools test: Endtest’s “Analyze failure with AI” explained a failed step in about 20 seconds, but on a flaky run it blamed the locator when the real problem was that the page never changed. AI explanations can save time; check them against the trace before you change the test. We plan to add a measured AI section after a second run.
What did we not test?
One small static page and one cause of flakiness (random load time). We did not test order-dependent tests, shared data, network problems, real CI machines, Firefox or WebKit, Selenium or Cypress, or any AI tool on this switch. 80 runs per setup gives rough percentages, and the suite times come from a cloud container, not a benchmark.
Frequently Asked Questions
What is a flaky test?
A flaky test passes and fails on the same code without any change. The most common cause in UI tests is timing: the test checks the page before it is ready.
Are flaky tests the same as brittle tests?
No. A brittle test breaks after a small, harmless change in the app, such as a renamed button. A flaky test changes its result with no change at all.
Should I just add retries?
Only together with the flaky report. In our run, 2 retries still let the hard-wait test fail 19 of 80 times and made the suite 39 to 50% slower. Use the list of tests that passed only on a retry as your fix list.
How do I find flaky tests in Playwright?
Run the test many times with –repeat-each, for example 40. In CI, turn on retries and look for tests marked “flaky” in the HTML or JSON report: Playwright uses that label for tests that failed first and passed on a retry.
What is the fastest fix for a timing-related flaky test?
Replace the fixed wait with a web-first assertion that waits for the condition you need, with a timeout longer than the slowest real load. In our run that took the failure rate from 64% to 0%.
Sources
- Our runs: AITG Demo Shop v3.0.0, Playwright 1.63.0, 30 November 2026 (setup above).
- Playwright docs: Retries (passed, flaky and failed categories), checked 30 Nov 2026.
- Playwright docs: Assertions (auto-retrying assertions, 5-second default timeout), checked 30 Nov 2026.
Get new hands-on tests by email
A short email when we publish a new hands-on test, plus our free LLM attack test set. No spam.





