Flaky tests pass and fail intermittently without code changes. Systematic diagnosis in Playwright — traces, shared state, network variability, and locator ambiguity — turns intermittent red builds into permanently green ones.
Common Flake Causes
Race conditions from missing awaits, order-dependent shared state, live network unpredictability, time/date dependencies, and ambiguous locators matching wrong elements.
CI-specific flakes often involve slower CPUs, missing env vars, parallel collision on shared resources, or timezone differences.
1. Open trace from failed CI run
2. Check missing await / wrong locator
3. Stub network with page.route
4. Reset state in beforeEach
5. Remove waitForTimeout
Each test creates its own data or uses unique IDs. No test reads another test's leftover cart or user. Database reset or transaction rollback in global teardown if needed.
Common Mistakes
Increasing timeout until flake rate drops slightly.
Retries without ever opening trace.
Running against shared staging with other teams' data.
Date.now() assertions without mocking clock.
Key Takeaways
Flakes are fixable — avoid normalizing them with retries only.
Traces and repeat-each reproduce and explain failures.
Isolation and network control eliminate most flakes.
Web-first assertions fix race-heavy patterns.
Pro Tip
Quarantine flaky tests with test.fixme and a linked ticket — don't let them run in main CI until fixed, or they erode team trust in the whole suite.
Continue with Playwright Fixtures to build on what you learned here.