Playwright Tests Failing in CI but Passing Locally: Causes and Fixes

A Playwright test passes on a developer laptop, then fails on GitHub Actions or GitLab CI. The first reaction is often to increase the timeout or add a retry. That can hide the symptom while leaving the cause untouched. The useful question is: what is different between the local run and the CI run?

Start with a reproducible example

Consider a checkout test that opens a product page, adds an item to the basket and expects the order summary to show one item. It passes when run alone. In CI, it fails intermittently with either a timeout waiting for the basket count or an unexpected quantity of two.

This is a realistic diagnostic scenario, not a claim about a specific customer incident. It contains two different failure signatures: a timing problem and possible shared state. Treating both as generic flakiness would send the investigation in the wrong direction.

1. Browser and runtime versions do not match

A local machine may use a browser installed weeks ago, while CI downloads the browser bundled with the Playwright package. A floating Docker tag can also change its underlying Node.js or browser runtime. The result can be different rendering, protocol behaviour or missing system dependencies.

Record the versions in both environments before changing tests:

npx playwright --version
node --version
npx playwright install --with-deps chromium

Use a Playwright Docker image compatible with the package version in your lockfile. Pin dependencies through the lockfile and avoid assuming that a mutable image tag is an immutable build. When a container is used, log its image reference and the browser version at the start of the job.

2. The test waits for time instead of a condition

A fixed sleep such as await page.waitForTimeout(1000) assumes the application will always finish within one second. A fast laptop may satisfy that assumption while a busy CI runner does not. Increasing the sleep to five seconds makes the suite slower and still does not guarantee correctness.

Wait for the user-visible result instead:

await page.getByRole('button', { name: 'Add to basket' }).click();
await expect(page.getByTestId('basket-count')).toHaveText('1');

Playwright assertions retry until the condition is met or the timeout expires. Prefer accessible roles and labels where possible, and use test IDs for stable application-specific state when a semantic locator is not suitable.

3. Parallel workers share the same test data

The unexpected quantity of two in the checkout example may not be a browser timing issue. Two workers could be using the same account or basket. One test adds an item while another test changes the same basket. Each test is valid in isolation, but the suite is not isolated.

Give each test a unique user, order or namespace. If creating a user is expensive, use a worker-scoped account only when tests do not mutate shared state, and create per-test resources for mutable workflows. Make cleanup explicit and safe to repeat.

4. CI is slower or more resource constrained

Running eight browser workers on a two-core runner can increase contention rather than reduce duration. The database, application server or test environment may also be the bottleneck. Compare the same test at one worker and at the normal worker count. If failures appear only under concurrency, investigate shared resources and capacity before changing assertions.

// playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
  retries: process.env.CI ? 1 : 0,
  workers: process.env.CI ? 2 : undefined,
  reporter: [['html', { open: 'never' }]],
  use: {
    trace: 'on-first-retry',
    screenshot: 'only-on-failure',
    video: 'retain-on-failure'
  }
});

These are starting settings, not universal optimums. Choose worker count from runner capacity and observed throughput. Keep first-attempt failures visible even when a retry passes.

5. CI does not preserve enough evidence

A log line saying “expect received 0” rarely explains why the application was in that state. Retain the Playwright trace, screenshot, browser console output and relevant server logs. Upload reports even when the test step fails, otherwise the most useful diagnostic evidence may disappear with the job.

For GitHub Actions, make artifact upload run after a failed test step using an always-style condition, and set a retention period that matches your team's investigation needs. Do not put credentials, session tokens or customer data into traces that are accessible beyond the intended team.

A practical triage sequence

  1. Compare Playwright, Node.js, browser and container versions.
  2. Run the failing test alone in CI and locally with the same configuration.
  3. Run with one worker, then with normal parallelism.
  4. Check whether the failure is timing, shared state, environment or a genuine product regression.
  5. Inspect the trace and logs from the first failed attempt.
  6. Fix the underlying cause, then keep a regression test for that failure mode.

Do not use retries as the definition of reliability

A retry can help confirm an intermittent failure, but a test that passes only on its second attempt is still producing an unreliable signal. Track first-attempt pass rate, retry rate, duration and failure categories separately. A green final status alone can conceal a deteriorating suite.

When the problem is bigger than one test

If failures span several specs, teams or pipeline stages, inspect the automation system rather than patching each test independently. Test architecture, fixtures, data isolation, browser setup, CI parallelization and reporting often interact. An independent QA automation audit can review one repository and one primary CI pipeline, document evidence-backed risks and prioritize practical improvements. See the sample report to understand the expected deliverable.

QA AUTOMATION HEALTH CHECK

Find the cause behind unreliable CI results.

Get an independent review of test reliability, automation architecture and CI, with prioritized findings and a practical 30-day plan.

Explore the review →