Skip to content
TestLine Menu
TestLine
automation

Flaky tests: where they come from and how to fix them

Seven causes of flaky E2E tests, a way to find them, Playwright fix examples and team rules that stop them coming back.

author
Artur Machowski, Senior QA Engineer
published
reading
7 min

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

causesymptomfix
1. waiting for time, not statefails on a slower CI, passes locallywait for an element or a response, e.g. await expect(...).toBeVisible()
2. brittle selectorsfails after a visual change, even though the feature worksgetByRole, getByLabel, getByTestId instead of CSS classes and nth-child
3. shared datafails when run in paralleleach test creates its own data with a unique identifier
4. order dependencepasses in the suite, fails on its own, or the other way roundeach test sets up its own state, new browser context
5. external servicestimeouts on payment, email, partner APIsandbox or a stubbed response (page.route), separate integration tests
6. dates and time zonesfails at midnight, at month end, after a clock changefixed clock in the test (page.clock), fixed time zone in the config
7. overloaded environmentfails when CI runs many jobs at oncefewer 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. @flaky runs 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.

Frequently asked questions

What is a flaky test?

A test that passes one run and fails the next with the same code and the same environment. Its result depends on timing, order, data or load, not on whether the application works.

What is the most common cause of flaky E2E tests?

Waiting for time to pass instead of for the application's state, e.g. a fixed 3 seconds before a click. On a fast laptop the test passes, on a slower CI it doesn't. The second common cause is data shared by several tests.

Do automatic retries solve the problem?

No, they only hide it. Retries make sense as a temporary safety net, but only when the report shows which tests passed on the second attempt and someone fixes them.

How many flaky tests is too many?

A good working limit is 1-2% of tests that pass only on retry. Above that, the team stops trusting a red pipeline and starts ignoring it.

Should I delete a flaky test?

First move it to quarantine with an owner and a fix date. Delete it if it still can't be stabilised after the fix, and what it checks can be tested at a lower level, e.g. with an API test.

Got a release in a few weeks?

Describe your project in the form. I'll reply with questions about scope or an initial test plan.