Playwright + TS · Lesson 34 of 36

0/36 done

Module 34: Retries & Flaky Test Management

34. Root-Cause Analysis & Flaky Test Elimination

17 minlesson

Try it yourself

Implement classifyTestResult:

  1. Accept attempts (number) and finalPassed (boolean).
  2. Return 'FAILED' if finalPassed is false; 'FLAKY' if attempts > 1 and finalPassed is true; otherwise 'PASSED'.

The editor has a worked example. Read it, tweak it, then press Mark as complete.

Why Tests Become Flaky

  1. Hard Sleeps (`waitForTimeout`): Relying on arbitrary sleep timers instead of actionability checks.
  2. Shared State: Test A mutating data that Test B depends upon.
  3. Fragile Locators: Relying on generated CSS classes that mutate between builds.
  4. Animation Latency: Clicking an element while it is sliding into view.

Retry Configuration in Playwright

typescript
export default defineConfig({
  // Retry 2 times on CI, 0 times locally
  retries: process.env.CI ? 2 : 0,
});

When a test fails on attempt 1, passes on attempt 2:

  • Playwright marks the test as Flaky (yellow status).
  • Captures full traces of the failed attempt for inspection.

Stress Testing for Flakiness

Before merging a new test into the main branch, run it in a loop 20 times:

bash
npx playwright test tests/checkout.spec.ts --repeat-each=20

If it fails even once, fix the locator or synchronization before merging!

Key Takeaways & Walkthrough Summary

Flaky tests erode team confidence in test automation. In this lesson, we will explore the root causes of flakiness, configure intelligent retry strategies for CI pipelines, and use Playwright tools to identify, isolate, and permanently eliminate flaky tests. Let's build rock-solid stability!

Part of the Playwright + TS course (Playwright with TypeScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Is adding retries considered a cure for flaky tests?

No. Retries are a safety net to prevent blocking CI deploys on transient network glitches. Every test that retries should be diagnosed using Trace Viewer and permanently stabilized.

How do I disable animations globally to speed up tests?

You can inject CSS using page.addStyleTag({ content: '* { transition: none !important; animation: none !important; }' });

Finished reading?

Review the worked example in the editor, then mark the lesson complete.

TypeScript
Skip