Flaky tests: find the cause, fix it
What a flaky test is, the usual causes (fixed sleeps, shared data, fragile locators, timing), how to fix each one, and why retries are a safety net, not a cure.
Transcript
A flaky test passes sometimes and fails sometimes, with the same code and the same app. Flaky tests are worse than no tests, because the team stops trusting a red result.
The common causes: fixed sleeps, tests that share data or depend on each other, fragile locators, and slow servers or animations.
Fix the cause, not the symptom. Replace sleeps with web first assertions. Give each test its own data. Use role and label locators. And wait for the real signal, like a response or a visible message.
Retries can hide the problem. Use them in C I as a safety net, but when the report marks a test as flaky, treat it as a bug to fix, and do not just retry it away.
Remember: a flaky test is a bug in the test. Find the cause, fix it, and use retries only as a safety net.
More in this series
1:2711Debugging tools: UI mode, codegen, inspector, trace
Find out why a test fails: UI mode with a timeline of snapshots, codegen to draft locators, the inspector to step through, and the trace viewer for failures in CI.
1:2113Reports and running tests in CI
Run Playwright on every pull request with GitHub Actions, run tests in parallel across workers and browsers, and keep the HTML report and traces as artifacts.
1:3314Your automation plan
A practical plan for a manual tester becoming automation QA: start with ten smoke tests, build them well, triage failures as product bug, test bug or environment, and grow each sprint.