Roadmap › Test Automation — Selenium, Playwright, API and BDD with C# › Module: API, BDD and CI
Lessons in this course (10)
10. Flaky tests — causes and cures
Advanced★ Must knowLesson 10 of 10
A flaky test passes and fails on the same code. It destroys trust in the suite.
| Cause | Cure |
|---|---|
| Timing (element not ready, animations, async loads) | Explicit waits on conditions / Playwright auto-wait; wait for network idle or a specific response |
| Shared/test-order-dependent data | Each test creates and cleans its own data; unique names (GUIDs) |
| Brittle locators | data-testid, role-based locators, POM |
| Environment instability | Health check before run, mock external services (WireMock), stable test env |
| Parallel collisions | Separate users/data per worker; thread-safe driver handling |
| Hidden bugs (race conditions in the app) | Investigate — the flakiness may be real |
Process: quarantine (tag and exclude from blocking runs), track flake rate, fix or delete — never just add retries everywhere (retries hide real problems; use them as a temporary, visible measure).