Sphere
Log in Join

Why our Playwright e2e tests stopped being flaky after a config change

I spent most of last spring blaming our tests for being badly written. They were, a bit, but the thing that finally made them reliable was a config change that had nothing to do with locators.

We were on Playwright 1.44.1, running in CI on Ubuntu 22.04 with two vCPUs and no GPU. Locally everything passed. On CI roughly one run in six failed with a checkout test that timed out. The team had built up a habit of just hitting re-run, which is its own problem.

## The flake we pretended wasn't there

The failing test was the most boring one in the suite: fill a form, click submit, expect a confirmation heading. The assertion was fine. What happened was that the click sometimes landed before the client had hydrated, so the native form did a full POST to nowhere and the page reloaded. Then the test waited for a heading that would not appear.

Our first fix was a waitForTimeout(500). That worked for three days. Then it failed again, because on a loaded CI machine 500 ms is nothing.

## The actual error

When I finally got a failing run with DEBUG=pw:api on, the log was not subtle:

Error: locator.click: Timeout 30000ms exceeded.
Call log:
- waiting for locator('[data-testid="submit-order"]')
- locator resolved to <button data-testid="submit-order">Place order</button>
- attempting click action
- waiting for element to be visible, enabled and stable
- element is not stable
- waiting for element to be visible, enabled and stable
- element is not stable

Element is not stable, meaning the button was moving. Our cookie banner had an entrance animation that finished at a slightly different time depending on how busy the machine was, and it pushed the checkout form down by about 46px. Playwright was doing exactly the right thing by refusing to click a moving target.

## What we changed in config

Two things. First, we stopped starting the app from a shell script in the CI step and let Playwright own it:

ts
// playwright.config.ts
export default defineConfig({
retries: process.env.CI ? 2 : 0,
reporter: process.env.CI ? [['github'], ['html', { open: 'never' }]] : 'list',
use: {
baseURL: 'http://127.0.0.1:3000',
trace: 'on-first-retry',
screenshot: 'only-on-failure',
reducedMotion: 'reduce',
},
webServer: {
command: 'pnpm dev --port 3000',
url: 'http://127.0.0.1:3000',
reuseExistingServer: !process.env.CI,
timeout: 120_000,
},
});

The old setup had a race where the CI step thought the server was up after the port opened, before the app had finished compiling its first route. page.goto then got net::ERR_CONNECTION_REFUSED on the very first navigation. The url health check in webServer waits for an actual response, not just a listening socket.

Second, we set the consent key in localStorage with an init script before any page code runs. That is a test fixture, not a product hack.

## Retries are a debugging tool, not a fix

retries: 2 did not make the tests pass. What made them pass was being able to open the trace from the one run in six that failed and see the exact frame where the button moved. trace: 'on-first-retry' records DOM snapshots, network and console for the retry, which is the attempt most likely to reproduce under the same load.

Between the trace, the storage fixture and reducedMotion, the checkout test has been green for about seven weeks. There is still a test that occasionally times out when the CI runner is doing something else, and I have not worked out whether that one is us or the runner. If you have a good way to tell, I am all ears.