Playwright testing on every PR: a CI skeleton that fails closed

Copy-paste Playwright config and GitHub Actions job for a green/red PR gate, plus one flake pattern and how to kill it.

You need a green/red signal on every pull request. Not a 200-test suite. One critical path that blocks merge when it fails.

Below: a minimal playwright.config.ts, a GitHub Actions job that fails closed, and one flake pattern with the fix. Official CI notes: Playwright CI guide.

Define the PR signal before you write tests

Pass means: a known user can complete one money path on staging. Fail means: do not merge.

Write that as one acceptance line and pin it in the PR template or the workflow comment. Example:

Given a seeded [email protected] user, login → create one invoice → land on /invoices/:id with status Draft.

In the gate Out of the gate
Login + one create + assert URL/state Full regression matrix
One browser project (Chromium) Cross-browser matrix on every PR
Staging URL from secrets Localhost-only runs that skip in CI
Trace on first retry Silent retries with no artifact

If you cannot name the path in one sentence, the suite will grow sideways and the PR signal will stop meaning anything.

Minimal playwright.config.ts for CI

Use env for the target. Keep retries only in CI, and turn traces on for the first retry so a flake leaves evidence. Retries without a trace hide the bug; see test retries and Trace Viewer.

TypeScript
import { defineConfig, devices } from '@playwright/test';

const baseURL = process.env.PLAYWRIGHT_BASE_URL;
if (!baseURL) {
  throw new Error('PLAYWRIGHT_BASE_URL is required');
}

export default defineConfig({
  testDir: './e2e',
  fullyParallel: true,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  workers: process.env.CI ? 2 : undefined,
  reporter: [['list'], ['html', { open: 'never' }]],
  use: {
    baseURL,
    trace: 'on-first-retry',
    screenshot: 'only-on-failure',
  },
  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    },
  ],
});

Locally you can point PLAYWRIGHT_BASE_URL at a docker compose stack. In CI it must be staging (or an ephemeral preview). If the env var is missing, the config throws — that is intentional. A skipped suite that reports green is worse than a red job.

GitHub Actions job that fails closed

This workflow runs on every pull request, installs browsers, runs the gate, and uploads the HTML report when anything fails.

YAML

Do not add continue-on-error: true on the test step. Do not exit 0 when staging is down. If staging is broken, the gate should fail — that is the product signal.

Seed data belongs outside this YAML (fixture script, shared staging tenant, or a setup project). Keep that separate so the gate file stays readable.

One flake pattern and the fix

This is the failure mode that shows up after you move the same test from a laptop to Actions: cold staging, parallel workers, slower navigation.

Broken — fixed sleep after submit

TypeScript
import { test, expect } from '@playwright/test';

test('create invoice (flaky)', async ({ page }) => {
  await page.goto('/login');
  await page.getByLabel('Email').fill('[email protected]');
  await page.getByLabel('Password').fill(process.env.E2E_PASSWORD!);
  await page.getByRole('button', { name: 'Sign in' }).click();

  await page.goto('/invoices/new');
  await page.getByLabel('Customer').fill('Acme');
  await page.getByRole('button', { name: 'Create' }).click();

  await page.waitForTimeout(3000); // fails when staging is slow
  await expect(page.getByRole('heading', { name: /Invoice/i })).toBeVisible();
});

Fixed — wait on the outcome Playwright can observe

TypeScript
import { test, expect } from '@playwright/test';

test('create invoice', async ({ page }) => {
  await page.goto('/login');
  await page.getByLabel('Email').fill('[email protected]');
  await page.getByLabel('Password').fill(process.env.E2E_PASSWORD!);
  await page.getByRole('button', { name: 'Sign in' }).click();
  await page.waitForURL('**/dashboard');

  await page.goto('/invoices/new');
  await page.getByLabel('Customer').fill('Acme');
  await page.getByRole('button', { name: 'Create' }).click();

  await page.waitForURL('**/invoices/**');
  await expect(page.getByRole('heading', { name: /Invoice/i })).toBeVisible();
  await expect(page.getByText('Draft')).toBeVisible();
});

If the UI stays on the same URL and only a status chip changes after an async job, use expect.poll against that chip or against a GET you control — still no waitForTimeout.

Retries (config above) buy you a second attempt. They do not replace a correct wait. Open the trace from the uploaded report when a retry saved the run; that is the bug you still owe.

When this gate is not enough

This PR gate catches broken critical UI paths. It does not replace:

  • API authorization and idempotency checks — a green browser flow can still leave a BOLA hole on /accounts/{id}. See payment API idempotency testing.
  • Load / arrival-rate behaviour — Playwright will not show coordinated omission under traffic. Use a proper load tool; start from realistic k6 load tests.

Keep the E2E gate thin. Add depth in API and performance layers, not by growing the Chromium job until every PR waits twenty minutes.

Choosing the runner itself (and when Selenium should stay) is covered in Playwright vs Selenium.

If you want the same skeleton mapped onto your repo and staging, request the free 20-minute check-up: you get a short written list of gaps in the PR gate (env, seed, waits, report upload) — not a sales deck. For scoped automation work, see services.

Want a second pair of eyes on your system?

Assertwise lab: Playwright CI job summary card
Lab sample CI layout ? not a client pipeline.

Request the free 20-minute check-up: short written findings, non-intrusive, no obligation.

Free 20-min check-up →

Next: flake quarantine policy (decision table, traces, forbidden waits).