Prompt
I'm trying to reduce flaky end-to-end tests in CI; suggest a practical workflow and tools to improve stability.
Latest observation
Here’s a practical, CI-friendly workflow to reduce flaky end-to-end tests, plus a tool stack that usually helps a lot.
1) Start by treating flakiness as a product of the test system, not “randomness”
Track every flaky failure with enough context to classify it:
- test name
- error type
- browser/device
- commit SHA
- timing data
- retry count
- screenshots/video/logs
Goal: identify whether failures come from:
- async timing/race conditions
- data/setup issues
- environment instability
- selector brittleness
- external dependencies
- true product bugs
2) Make tests deterministic by controlling the environment
A lot of flaky E2E issues disappear when the test environment is predictable.
Do:
- run tests in containers
- pin browser versions
- use seeded/randomized test data generation
- reset DB/state before each run
- mock or stub third-party services where possible
- use fixed clocks/timezones/locale when relevant
- isolate tests from shared infrastructure
Tools:
- Docker / Docker Compose
- Testcontainers for spinning up dependencies
- Ephemeral CI environments or preview environments
- Mock Service Worker (MSW) for network mocking in browser tests
- WireMock / MockServer for API stubbing
3) Prefer explicit waiting over arbitrary sleeps
Flaky tests often come from timing assumptions.
Avoid:
sleep(5)- waiting on page load only
- asserting before the UI is ready
Prefer:
- wait for a specific DOM element/state
- wait for network response
- wait for a job/queue completion signal
- wait for an app-specific “ready” condition
Good practice:
- use built-in auto-waiting if your framework supports it
- assert on visible/interactive state, not just presence in DOM
Tools:
- Playwright: excellent auto-waiting and stable locators
- Cypress: built-in retrying assertions and command queueing
- Testing Library: query by role/text/label instead of CSS selectors
4) Use robust selectors
Brittle selectors are a huge source of CI failures.
Prefer:
- accessibility roles
- labels
- test IDs only when necessary
Avoid:
- deep CSS paths
- nth-child selectors
- text that changes often
- selectors tied to layout implementation
Tools:
- Testing Library queries
- Playwright locators
- Cypress Testing Library
5) Stabilize test data and state
Tests that depend on shared data or previous test runs often fail intermittently.
Workflow:
- create data inside the test or in setup hooks
- use unique IDs per test run
- avoid cross-test dependencies
- clear caches/session/local storage
- make teardown reliable, even on failure
Tools:
- test fixtures/factories
- DB reset scripts
- API-based setup/teardown
- Cypress/Playwright fixtures
6) Reduce external dependency risk
If your E2E tests hit third-party systems, flakiness will go up.
Strategy:
- stub external APIs in CI
- test third-party integrations separately with contract tests or sandbox environments
- keep a small set of true integration tests that run less frequently
Tools:
- Pact for contract testing
- MSW/WireMock/MockServer
- provider sandboxes
7) Add retries, but only as a mitigation
Retries can reduce noise, but they should not hide real instability.
Best practice:
- allow 1 retry for CI signal reduction
- record retry occurrence as technical debt
- automatically open an issue or flag flaky tests repeatedly failing
- do not rely on retries as the primary fix
Tools:
- built-in retries in Playwright, Cypress, Jest
- CI annotations/metadata for flaky test tracking
8) Quarantine and fix flaky tests quickly
Create a process so flaky tests don’t linger.
Workflow:
- mark flaky tests as quarantined
- keep them visible in a dashboard
- assign an owner
- set an SLA for repair
- remove quarantine once stable
Tools:
- CI test reports
- flaky test dashboards
- issue tracker labels
- custom scripts to track recurring failures
9) Improve observability for debugging failures
The faster you can see what happened, the faster you can fix it.
Capture on failure:
- screenshots
- video
- console logs
- network logs
- trace files
- DOM snapshots
Tools:
- Playwright Trace Viewer
- Cypress videos/screenshots
- Allure Report
- ReportPortal
- browser logs via CI artifacts
10) Run tests in layers
Don’t push everything into E2E.
Recommended test pyramid-ish approach:
- Unit tests: fast logic checks
- Integration tests: service/component boundaries
- Contract tests: API compatibility
- E2E tests: critical user journeys only
This reduces the number of slow, fragile E2E tests and makes failures easier to diagnose.
A practical CI workflow
On every PR
- run a small, critical E2E smoke suite
- use isolated test data
- collect screenshots/video/trace on failure
- retry once max
- fail fast if core journey breaks
Nightly
- run the full E2E suite
- shard across workers for speed
- run against the latest browser versions
- collect flake metrics
Weekly
- review flaky test trends
- delete redundant E2E coverage
- move unstable checks down to integration/contract tests
- refactor shared setup/fixtures
Recommended tool combinations
If you’re starting fresh
- Playwright for E2E
- Docker for environment consistency
- MSW for API stubbing
- Allure or built-in CI artifacts for reporting
- Pact if APIs are shared across teams
If you’re already on Cypress
- Cypress retries
- Cypress Testing Library
- screenshots/videos in CI
- MSW/WireMock for controlled network behavior
- keep selectors accessible and avoid brittle waits
If you need backend dependency realism
- Testcontainers
- PostgreSQL/Redis/Kafka containers
- seeded fixtures and reset scripts
Quick wins that usually help immediately
- Replace
sleep()calls with explicit waits. - Switch selectors to role/label-based queries.
- Reset test data per run.
- Stub unstable third-party services.
- Run tests in containers with pinned versions.
- Capture trace/video on every failure.
- Add one retry and track flake rate.
- Split critical smoke tests from full regression suite.
If you want, I can also give you:
- a Playwright-specific stability checklist
- a Cypress-specific workflow
- or a sample CI pipeline showing retries, artifacts, and quarantining.