πŸ’‘ If you like this website, please share it with your friends and network! πŸš€
Back to All Questions
Question 42 of 100
Execution
Intermediate

How to repeat a test in Playwright irrespective of pass or fail?

The Answer

Use `test.describe.configure({ retries: N })` or the global `retries` config to repeat tests. For guaranteed repetition regardless of outcome, use a loop inside the test or the `--repeat-each` CLI flag.

Deep Dive Explanation

`--repeat-each` is excellent for identifying flaky tests before merging to main. Run your test suite with `--repeat-each=5` in a dedicated flakiness pipeline. Any test that fails even once across the 5 runs is considered flaky and needs investigation.

example.spec.ts
// Run every test 3 times via CLI (useful for flakiness detection)
// npx playwright test --repeat-each=3

// Or in config
export default defineConfig({
  repeatEach: 3,
});

// Custom loop inside a test
test('stability check', async ({ page }) => {
  for (let i = 0; i < 5; i++) {
    await page.goto('/');
    await expect(page.getByRole('heading', { level: 1 })).toBeVisible();
    console.log(`Run ${i + 1} passed`);
  }
});