← Back to libraryQuestion 300 of 468
🏗️Framework DesignAdvanced

Handling Flaky Tests at the Framework Level

📌 Definition:

Flaky tests pass and fail non-deterministically, eroding trust. A framework reduces flakiness by design — robust waits, stable locators, isolated data, controlled network — and tracks/quarantines remaining flakes rather than masking them with blanket retries.

📖 Detailed Explanation:

Root causes of flakiness: fixed sleeps/timing races (fix with explicit waits), brittle locators (use stable data-* attributes), shared/dirty test data (isolate + seed + clean), external dependencies (stub/mock or use API setup), animation/async races, and parallel collisions (thread-local drivers, unique data). The framework should provide the tools (wait helpers, data factories, stable-locator conventions) that make reliable tests the default. Retries can absorb rare irreducible noise but must not hide reproducible bugs; track flaky tests (report analytics), quarantine and fix the worst offenders. Flakiness management is a defining senior-level topic.

🔑 Key Points:
  • Root causes: timing races, brittle locators, dirty data, externals, parallelism
  • Fix by design: explicit waits, stable locators, isolated data, stubbing
  • Retry sparingly — don't mask reproducible bugs
  • Track/quarantine flakes; fix top offenders
🌍 Real-World Example:

The framework enforces explicit-wait helpers, data-testid locators, per-test unique data, and API-stubbed externals; a flaky-test dashboard surfaces the worst offenders, which the team fixes at the root instead of cranking up retries.

🎯 Scenario-Based Interview Question:

Your suite is 'green only with 3 retries'. Walk through how you'd address flakiness properly.