Prompt
How do I troubleshoot browser automation that passes locally but fails in CI?
Latest observation
When browser automation passes locally but fails in CI, the cause is usually one of these: timing, environment differences, missing dependencies, viewport/headless differences, or flaky tests that only appear under slower CI conditions.
Here’s a practical troubleshooting checklist.
1) Compare local vs CI environment
Check for differences in:
- Browser version and driver version
- Operating system / container image
- Headless vs headed mode
- Viewport size and device scale factor
- CPU / memory limits
- Locale / timezone / language
- Network speed / latency
- Environment variables and secrets
- Test data state and backend dependencies
A test that assumes a 1920×1080 window or fast network often breaks in CI.
2) Make failures observable
In CI, collect artifacts on every failure:
- Screenshots
- Video recordings
- Browser console logs
- Network logs / HAR files
- DOM snapshots / page source
- Trace files if your framework supports them
Examples:
- Playwright: use tracing, screenshots, video, and console event listeners.
- Cypress: save screenshots/videos and inspect command logs.
- Selenium: capture screenshots and browser logs, plus page source.
These artifacts usually reveal whether the app didn’t load, the selector changed, or the test simply moved too fast.
3) Increase visibility into timing issues
CI is often slower and more variable. Common symptoms are:
- element not found
- click intercepted
- stale element reference
- timeout waiting for navigation or animation
Fixes:
- Prefer explicit waits for app state over fixed sleeps.
- Wait for:
- element visible and enabled
- network idle
- loading spinner disappears
- URL change
- specific API response
- Avoid brittle waits for exact timing.
If the test only passes with sleep(2), it’s a sign you need a state-based wait.
4) Check headless-specific behavior
Some apps behave differently in headless mode:
- different rendering
- missing fonts
- popovers/dialogs positioned differently
- scrolling issues
- autoplay/video restrictions
- focus/hover behavior changes
Try:
- running CI in headed mode inside a debug environment
- matching local to CI by running locally in headless mode
- forcing a consistent viewport and device settings
5) Look for flaky selectors
Selectors that depend on:
- CSS classes generated at runtime
- text that changes
- layout order
- nth-child position
- fragile XPath
Prefer:
- stable
data-testid/data-testattributes - accessible roles and labels
- IDs that are not auto-generated
If the DOM changes slightly in CI, brittle selectors often fail first.
6) Validate test isolation
Tests may pass locally because your machine has leftover state.
Check whether CI runs:
- in a clean profile
- with cleared cookies/localStorage/sessionStorage
- against a clean database or unique test tenant
- with no dependency on test order
Common problems:
- a previous test logged the user in
- data created in one test isn’t cleaned up
- parallel tests collide on the same account/resource
7) Inspect browser console and network errors
CI failures may actually be app failures:
- JavaScript exceptions
- failed asset loads
- 401/403/500 API responses
- CORS issues
- CSP blocks
- certificate issues
- blocked third-party resources
Add logging for:
console.error- page errors / uncaught exceptions
- failed network requests
- HTTP status codes
8) Check for missing system dependencies
In containers or CI images, browsers may fail due to missing libs/fonts:
- fonts not installed
- sandbox restrictions
- missing shared libraries
- missing X11/GTK dependencies
- limited
/dev/shm
Symptoms include:
- browser won’t launch
- blank pages
- crashes under load
- screenshots render incorrectly
If using Docker, ensure the image includes all browser dependencies, or use the official browser-ready image.
9) Reproduce CI locally
This is often the fastest route to a fix.
Try to match CI as closely as possible:
- same Docker image
- same browser version
- same headless mode
- same env vars
- same test command
- same viewport and locale
If CI uses a container, run that container locally and execute the same test suite there.
10) Reduce parallelism temporarily
Parallel execution can expose race conditions or resource contention:
- shared user accounts
- shared files/downloads
- same port or database
- same backend records
- CPU starvation
Try running:
- one test at a time
- one worker
- with retries disabled
- with tests ordered deterministically
If the failure disappears, you likely have a concurrency issue.
11) Use retries only as a signal, not a fix
Retries can hide flaky tests. They’re okay short-term to reduce noise, but you should still:
- track flake rate
- quarantine unstable tests
- fix root cause
- remove retries after stabilization
12) Common root causes by symptom
“Element not found”
- page not fully loaded
- wrong selector
- responsive layout changed in CI
- iframe/shadow DOM issue
“Click intercepted”
- overlay/spinner still present
- animation not finished
- element offscreen
- different viewport
“Timeout waiting for navigation”
- app uses SPA routing
- navigation happens after async request
- CI is slower
- test waits for the wrong condition
“Works locally, fails only in Docker/CI”
- missing dependencies
- font/rendering differences
- sandbox or shm issues
- headless-only bug
13) Add a minimal debug harness
If possible, make failing tests dump:
- current URL
- page title
- screenshot
- HTML source
- console logs
- failed requests
- timing markers
That makes CI failures much easier to diagnose without rerunning interactively.
14) Build for robustness
Long-term improvements:
- Use reliable selectors
- Wait on app state, not arbitrary time
- Seed test data explicitly
- Isolate test accounts and resources
- Keep tests independent
- Run a subset in CI regularly and the full suite nightly if needed
- Use browser automation best practices from your framework
If you want, I can also give you:
- a framework-specific checklist for Playwright/Cypress/Selenium, or
- a CI debug template you can drop into your tests to capture screenshots, logs, and traces on failure.