← All postsAutomation

Five reasons your Playwright tests are flaky, and the fixes

Trainer, Automation track7 Oct 20267 min read

Most flakiness comes from timing assumptions. Here is how auto-waiting, web-first assertions and test isolation remove it.

A flaky test passes and fails on the same code. Teams stop trusting the suite, and real bugs slip through. In our client audits, five causes explain almost every case.

1. Fixed waits instead of web-first assertions

page.waitForTimeout(3000) guesses how long the app needs. Replace it with expect(locator).toBeVisible(), which retries until the condition is true or the timeout is reached.

2. Tests that share state

If test B depends on data that test A created, running them in parallel or in a different order breaks B. Give every test its own user and data through fixtures.

3. Fragile locators

CSS chains like div > div:nth-child(3) break whenever the layout changes. Use getByRole, getByLabel and getByTestId, which match how users and assistive technology see the page.

4. Unmocked third-party calls

Payment gateways, maps and analytics are slow and unpredictable in test environments. Route them with page.route() and return a fixed response.

5. Ignoring the trace

Turn on trace: on-first-retry. The trace viewer shows the DOM, network and console at every step, which usually points straight at the cause.

Go deeper

Test Automation with Playwright

From manual to automation. Build a Playwright + TypeScript framework and run it in CI.

See the course
Chat on WhatsApp