Short answer
A flaky test is one that passes one run and fails the next with the same code. In E2E tests the most common causes are waiting for time instead of for the application’s state, brittle selectors, shared test data, dependence on test order, external services, dates and an overloaded CI environment. Fixing starts with measuring: run the suspect test 20-50 times and see which step fails. A good working limit is fewer than 1-2% of tests that pass only on retry.
The 7 most common causes
| cause | symptom | fix |
|---|---|---|
| 1. waiting for time, not state | fails on a slower CI, passes locally | wait for an element or a response, e.g. await expect(...).toBeVisible() |
| 2. brittle selectors | fails after a visual change, even though the feature works | getByRole, getByLabel, getByTestId instead of CSS classes and nth-child |
| 3. shared data | fails when run in parallel | each test creates its own data with a unique identifier |
| 4. order dependence | passes in the suite, fails on its own, or the other way round | each test sets up its own state, new browser context |
| 5. external services | timeouts on payment, email, partner API | sandbox or a stubbed response (page.route), separate integration tests |
| 6. dates and time zones | fails at midnight, at month end, after a clock change | fixed clock in the test (page.clock), fixed time zone in the config |
| 7. overloaded environment | fails when CI runs many jobs at once | fewer parallel workers, a more stable test environment |
How to find a flaky test
Repeat it many times. In Playwright one flag is enough:
npx playwright test tests/cart.spec.ts --repeat-each=30
If the test fails even once in 30 runs, it’s flaky. Also run it with several workers at once (--workers=4), because some data problems only show up in parallel.
Collect a trace on retry. Playwright marks tests that passed only on retry as “flaky” in the report, and the trace shows every step with a screenshot and network traffic:
// playwright.config.ts
import { defineConfig } from "@playwright/test";
export default defineConfig({
retries: process.env.CI ? 2 : 0,
use: { trace: "on-first-retry" },
});
Look at the CI history. A test that fails once a week on different branches is almost always flakiness, not a bug in the code.
A fix example
A typical time-dependent test:
// flaky: fixed pause and a class selector
await page.click(".btn-primary");
await page.waitForTimeout(3000);
expect(await page.textContent(".toast")).toBe("Saved");
The same check, but waiting for the application’s state:
// stable: button role and an assertion that waits by itself
await page.getByRole("button", { name: "Save" }).click();
await expect(page.getByText("Saved")).toBeVisible();
Playwright assertions like toBeVisible keep retrying until the condition is met or the timeout runs out. That’s why fixed pauses are almost never needed.
Team rules
- A flaky test is a defect. It has a ticket, an owner and a fix date.
- Quarantine instead of ignoring. A test tagged e.g.
@flakyruns separately and doesn’t block releases, but only while it’s being fixed, e.g. for two weeks. - Retries only with a report. Someone checks every week which tests passed only on the second attempt.
- Don’t raise timeouts for all tests. It slows the whole suite down and hides real performance problems.
- Fewer E2E tests, more API tests. If something can be checked with an API test, it will be faster and more stable than through the browser.
I also write briefly about flaky tests in the pipeline in regression in CI. How to set up a test project from the start so there is less flakiness is covered in how to start regression automation in Playwright.
How I can help
If your test suite gets in the way more than it helps, I can review and stabilise it as part of test automation: find the flaky tests, fix the causes and set up rules in CI. Work on an existing suite is billed hourly, PLN 200-250 net per hour. Describe your test suite and tool in the contact form.