Prompt
How do I debug flaky browser tests that pass locally but fail in CI?
Latest observation
Flaky browser tests usually come down to one of a few things: timing, environment differences, shared state, or test-order dependence. Here’s a practical way to debug them.
1) Reproduce the CI environment locally
The fastest way to debug is to make local runs look like CI:
- Run the same browser version and mode as CI
- Use the same viewport/device settings
- Match OS/container if possible
- Run headless if CI is headless
- Use the same test command and env vars
If CI uses Docker or a cloud runner, try running the tests in that same image locally.
2) Get more evidence from the failing test
Enable artifacts on failure:
- screenshots
- video
- browser console logs
- network logs / HAR
- DOM snapshots or HTML dumps
- test runner traces if your framework supports them
This often shows whether the issue is:
- a selector not found
- an element not clickable
- a redirect or auth problem
- a race where the UI hasn’t settled yet
3) Look for timing and synchronization bugs
The most common cause is asserting too early or clicking before the UI is ready.
Typical fixes:
- wait for the right condition, not just a fixed sleep
- wait for navigation to finish
- wait for the element to be visible, enabled, and stable
- wait for API responses or state changes that drive the UI
- avoid arbitrary
sleep()calls unless temporarily for debugging
Examples of fragile patterns:
- clicking immediately after an animation starts
- asserting on text before async rendering completes
- using
waitForSelectorbut not checking visibility or readiness
4) Check for shared state and test isolation problems
Tests may pass alone but fail in CI when run in parallel or in a different order.
Verify that each test:
- creates its own data
- doesn’t depend on state from previous tests
- cleans up after itself
- uses unique user accounts, IDs, emails, etc.
- resets cookies/local storage/session storage if needed
Common hidden dependency:
- a test assumes a record already exists because another test created it earlier
5) Investigate race conditions in the app itself
Sometimes the test is exposing a real app bug.
Examples:
- a loading spinner disappears before data is actually ready
- a toast blocks clicks intermittently
- state updates happen out of order
- API responses arrive in a different order in CI
If your app is flaky in production too, browser tests are doing you a favor.
6) Check CI resource constraints
CI is often slower or less stable than local machines.
Issues to consider:
- CPU starvation
- low memory
- slower disk/network
- limited GPU or disabled hardware acceleration
- browser startup differences
- viewport too small causing responsive layout changes
This can change:
- animation timing
- element positions
- lazy loading behavior
- whether elements are hidden behind overlays
7) Add retries carefully
Retries can reduce noise, but don’t use them to hide the problem.
Good use:
- temporary mitigation while you investigate
- capturing more logs on first failure
Bad use:
- masking real timing bugs indefinitely
If you add retries, still track failure rate and root cause.
8) Make failures deterministic
To debug a flaky test, try to force failure:
- run it 20–100 times in a loop
- run the full suite multiple times
- run tests in random order
- run with parallelism on and off
- throttle CPU/network if possible
If a test fails only under load or when run with others, that’s a strong clue.
9) Instrument the app and test
Add temporary debugging output:
- log key lifecycle events
- log API requests/responses
- log when UI state changes
- log when elements become clickable
- dump page HTML on failure
For browser automation, it’s often useful to log:
- current URL
- page title
- console errors
- failed network requests
- visible text around the failing element
10) Use stronger locators
Flaky selectors are common.
Prefer:
- data-testid / data-qa attributes
- accessible roles and labels
- stable IDs
Avoid:
- brittle CSS chains
- text that changes with localization or dynamic content
- selecting by nth-child unless unavoidable
11) Common CI-only causes
A few especially common differences:
- timezone/locale differences
- missing fonts
- headless browser behavior
- popup blockers or permission prompts
- auth/session expiration
- third-party services unavailable in CI
- test data collisions between parallel jobs
- caching differences
12) A good debugging workflow
- Re-run failing test in a loop locally.
- Capture screenshots/video/logs on failure.
- Compare local vs CI browser/environment.
- Check for timing assumptions.
- Check isolation and shared state.
- Inspect app/network logs for the exact failure point.
- Fix the root cause, not just the symptom.
13) If you want a quick checklist
Ask:
- Is the environment really the same?
- Is the test waiting for the right thing?
- Does it depend on prior state?
- Does it fail more under load or parallelism?
- Are selectors stable?
- Are there console/network errors?
- Is the app itself flaky?
If you want, I can also give you a framework-specific debugging guide for Playwright, Cypress, Selenium, or WebdriverIO.