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

How does Playwright handle flaky tests?

The Answer

Playwright provides multiple strategies: automatic retries, trace capture on failure, video recording, and built-in auto-waiting that eliminates the #1 cause of flakiness (timing issues).

Deep Dive Explanation

Most flakiness comes from 3 sources: 1) Timing (fixed by auto-waiting), 2) State leakage between tests (fixed by browser context isolation), 3) Environment instability (mitigated by retries + traces). The trace viewer is especially powerful β€” it shows exactly which DOM state existed at the moment of failure.

example.spec.ts
// playwright.config.ts - anti-flakiness config
export default defineConfig({
  retries: process.env.CI ? 2 : 0,
  use: {
    trace: 'on-first-retry',   // Capture trace on first retry
    video: 'retain-on-failure',
    screenshot: 'only-on-failure',
  },
});

// Detect flakiness: run each test 5 times
// npx playwright test --repeat-each=5

// Use test.fixme() to mark known flaky tests for investigation
test.fixme('known flaky - @JIRA-123', async ({ page }) => {
  // Will be skipped but noted in report
});