How to Retry a Failed Playwright Test in CI
Playwright retries a failed test through the retries option, which the config commonly sets only when running in CI.
Set retries in playwright.config.ts, usually process.env.CI ? 2 : 0, and turn on trace: on-first-retry so a flaky run leaves a trace you can open.
Steps
- Set
retriesin the Playwright config, gated onprocess.env.CI. - Enable
trace: on-first-retryto capture evidence of the flake. - Review the flaky count in the report and fix the underlying wait.
Config
playwright.config.ts
import { defineConfig } from '@playwright/test';
export default defineConfig({
retries: process.env.CI ? 2 : 0,
use: { trace: 'on-first-retry' },
});Open the trace of a flake
Terminal
npx playwright show-trace test-results/**/trace.zipGotchas
- Playwright reports tests that passed on retry as "flaky", so read that count, do not ignore it.
- Retries hide missing web-first assertions like
expect(locator).toBeVisible(); add the wait instead.
Related guides
How to Deflake a Test by Fixing Async Waits in CIDeflake a test by replacing fixed sleeps with polling assertions that wait for a real condition, removing the…
How to Retry a Failed Jest Test in CIRetry a failing Jest test in CI with jest.retryTimes so a transient failure gets another attempt, while you s…
How to Quarantine a Flaky Test in CIQuarantine a flaky test in CI by tagging it and excluding it from the blocking suite while a separate non-blo…