Skip to main content

Retries and flakiness

Why This Matters

Retries turn a red build green, and that is exactly the danger. A test that passes on its second attempt has told you something is wrong: the timing, the data, the environment, or the product. Playwright marks such a test as flaky. If nobody looks at flaky tests, the retry hides the problem until the day it fails three times in a row, usually right before a release.

What retries do

The config retries twice in CI and never locally:

retries: process.env.CI ? 2 : 0,

A test that fails is run again, up to two more times. The outcome:

AttemptsStatus
Passes first timepassed
Fails, then passes on a retryflaky
Fails every timefailed

The first retry also records a trace (trace: "on-first-retry"), so a flaky test always leaves evidence behind.

A real flaky run

This is the end of a real CI run of the example repository, from the day the hybrid test of lesson 20 was still in its first version:

  ✘   71 [firefox] › tests/module-4/hybrid.spec.ts:6:1 › a cart filled through the API shows up in the shop (844ms)
  ✓   72 [firefox] › tests/module-4/hybrid.spec.ts:6:1 › a cart filled through the API shows up in the shop (retry #1) (2.8s)
  ...
  ✘   99 [webkit] › tests/module-4/hybrid.spec.ts:6:1 › a cart filled through the API shows up in the shop (1.1s)
  ✓  100 [webkit] › tests/module-4/hybrid.spec.ts:6:1 › a cart filled through the API shows up in the shop (retry #1) (2.3s)

  1 failed
    [chromium] › tests/module-4/hybrid.spec.ts:6:1 › a cart filled through the API shows up in the shop
  2 flaky
    [firefox] › tests/module-4/hybrid.spec.ts:6:1 › a cart filled through the API shows up in the shop
    [webkit] › tests/module-4/hybrid.spec.ts:6:1 › a cart filled through the API shows up in the shop
  104 passed (1.6m)

One test, three browsers, three outcomes: failed in Chromium, flaky in Firefox and WebKit. On a laptop it passed every time. If Chromium had also been lucky on a retry, this run would have been green, and the bug would have stayed in the suite.

Three causes, found for real

The example repository found three different causes of flakiness in one afternoon. Each is common.

1. A value read too early. The hybrid test read the sandbox id with textContent() right after goto. On a fast machine the id was there; in CI the page still showed its placeholder "…". Fix: wait with a web-first assertion first (toHaveText(/^[\w-]{8,}$/)), then read.

2. A wait on the wrong thing. A visual test waited for getByRole("alert"), and Next.js has an invisible role="alert" on every page. The wait passed at once, and the screenshot caught the form mid-login. Fix: wait for the specific element, with its text.

3. Hydration. On a slow machine the test typed into the login form before React had attached to it. The text was in the field, but not in React’s state, and the form answered “Username is required.”. Fix: repeat the interaction until the form gives the expected answer, with toPass, or wait for a sign that the app is interactive.

What the three have in common: they pass on a fast machine and fail on a slow one. CI is usually the slow one.

Find the cause, do not raise the retries

When a test is flaky:

  1. Open the trace of the failed attempt (module 3). Compare it with the passing retry: what was different?
  2. Reproduce locally with stress: npx playwright test hybrid.spec.ts --repeat-each=20 --workers=4. Slow your machine down if needed: CPU throttling in Chromium, or Docker under emulation, as module 6 did by accident.
  3. Fix the cause. Almost always one of: a missing await, a value read instead of asserted, a wait on the wrong thing, shared data (lesson 33), or a real race in the product.
  4. Only then consider whether a retry is still needed.

Make flaky visible

  • The HTML report has a Flaky filter.
  • The CI summary of this repository (scripts/summary.mjs, module 8) lists flaky tests separately with a ⚠️, next to the failures.
  • failOnFlakyTests: true in the config makes a flaky test fail the run. Strict, and worth it for a suite that is otherwise stable: a flaky test then gets fixed the day it appears.

About toPass

toPass (used in the hydration fix) retries a block of code until every assertion in it passes. It is the right tool when an interaction can legitimately need a second go. It is the wrong tool for a test you do not understand: wrapping a mystery in toPass is a retry with extra steps. Always write a comment saying why the block may need to repeat.

Exercise

The branch exercise/flaky contains the first version of the hybrid test, unchanged:

git checkout exercise/flaky
npx playwright test tests/module-7/flaky.spec.ts --repeat-each=20 --workers=4 --project=chromium
  1. Does it fail on your machine? Try the same with --project=webkit, or in Docker (npm run test:visual’s Docker command with --project=chromium).
  2. Find the cause in the trace of a failed attempt (--trace on).
  3. Fix it without changing retries, and compare your fix with tests/module-4/hybrid.spec.ts on main.
  4. git checkout main when you are done.
Show suggested answer

On a fast laptop the test may pass all twenty times: that is what makes it dangerous. Under load, in WebKit, or in Docker under emulation, it fails with Received string: "…" (the id read before the page knew it), or later with 0 items in the cart (the page’s sandbox and the browser’s cookie were not the same one after the first visit).

The fix on main does three things: wait for the real id with toHaveText(/^[\w-]{8,}$/) before reading it, reload once after the first visit so every request uses the same shop_session cookie, and only then read the id. No retry setting was changed.