Prompt
Why do my Playwright tests pass locally but fail in CI?
Latest observation
This is usually caused by differences between your local machine and CI, not by Playwright itself. The most common reasons are:
1) Timing / race conditions
Your tests may rely on UI appearing “fast enough” locally, but CI is slower.
- Animations, network calls, rendering, or background jobs can take longer.
- A test might assert before the app is actually ready.
Fixes:
- Prefer Playwright’s built-in waiting:
await expect(locator).toBeVisible() - Avoid
waitForTimeout() - Wait for specific states/events instead of arbitrary delays
2) Environment differences
CI often runs with:
- different CPU/memory limits
- different OS/browser versions
- headless mode
- different locale/timezone/viewport
Fixes:
- Make local and CI browser versions match
- Set consistent viewport, locale, timezone, and permissions
- Run tests in the same mode locally as CI (
headless=true)
3) Flaky selectors
Selectors may work locally but fail in CI if the DOM changes slightly or renders differently.
Fixes:
- Use stable locators like:
getByRole()getByLabel()getByTestId()
- Avoid brittle CSS/XPath tied to layout or generated classes
4) Test data / backend state
CI might start with a clean database, different seeded data, or slower services.
Fixes:
- Ensure test data is seeded consistently
- Mock external APIs when possible
- Reset database/state between tests
5) Parallelism issues
Tests may pass alone locally but fail in CI when running in parallel.
- Shared accounts, files, ports, cookies, or backend state can collide.
Fixes:
- Make tests independent
- Avoid shared mutable state
- Use isolated fixtures or unique test data per worker
6) Missing dependencies in CI
CI may not have everything your app needs:
- fonts
- system libraries
- environment variables
- secrets
- browser dependencies
Fixes:
- Check CI logs for missing env vars
- Install Playwright browsers/deps properly
- Verify the same
.envvalues are available in CI
7) Headless-only issues
Some code behaves differently in headless browsers.
Fixes:
- Reproduce locally with headless mode
- Check if your app relies on focus, hover, or layout behavior that differs headless
8) Network or API instability
CI may hit slower or less reliable network endpoints.
Fixes:
- Stub/mock network calls where possible
- Add retries only as a last resort
- Ensure external services are stable and reachable
Best debugging steps
- Run the same command locally as CI
- Enable Playwright trace/video/screenshots
- Inspect CI logs and trace viewer
- Compare env vars, browser versions, and viewport
- Look for flaky tests by rerunning
Example config to help debug:
use: {
trace: 'on-first-retry',
screenshot: 'only-on-failure',
video: 'retain-on-failure',
}
If you want, I can help you diagnose the exact cause if you paste:
- the failing test
- your Playwright config
- the CI error output
Brands