Back to All Questions
Question 31 of 100
Framework
Intermediate
How do Retries work in Playwright?
The Answer
Playwright can automatically re-run failed tests a configured number of times. The entire test (including beforeEach hooks) is re-executed from scratch on each retry.
Deep Dive Explanation
Retries enable trace capture on failure: set `trace: 'on-first-retry'` to get a full trace only when a test fails and is being retried. This reduces storage overhead while ensuring you always have a trace for genuinely failing tests.
example.spec.ts
// playwright.config.ts
export default defineConfig({
retries: process.env.CI ? 2 : 0, // 2 retries in CI, 0 locally
});
// Per-test retry override
test('flaky network test', async ({ page }) => {
test.info().retry; // Get current retry number (0 = first run)
// ...
}, { retries: 3 });
// Check retry count inside a test
test('with retry logic', async ({ page }, testInfo) => {
if (testInfo.retry > 0) {
await page.goto('/clear-cache'); // Extra step on retry
}
});