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.

CauseCure
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 dataEach test creates and cleans its own data; unique names (GUIDs)
Brittle locatorsdata-testid, role-based locators, POM
Environment instabilityHealth check before run, mock external services (WireMock), stable test env
Parallel collisionsSeparate 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).