Playwright in GitHub Actions
Why This Matters
A suite that only runs on your laptop protects only your laptop. A suite that runs on every push, on a machine nobody configured by hand, protects everyone who changes the code. It also finds the problems your laptop hides: module 7 found three timing bugs that only showed up in CI.
What you will learn
- What every line of the example repository’s workflow does, and why it is there
- Why the tests run inside the Playwright Docker image
- What changes in your config when
CIis set
The workflow
The full file is .github/workflows/playwright.yml in the example repository. This lesson covers the test job; lesson 39 adds the shards and the report job.
name: Playwright tests
on:
push:
branches: ["**"] # every branch, so the exercise branches also produce reports and traces
pull_request:
workflow_dispatch:
jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 20
container:
image: mcr.microsoft.com/playwright:v1.65.1-noble
options: --user 1001
steps:
- uses: actions/checkout@v6
- run: npm ci
- run: npm run typecheck
- run: npm run lint
- run: npx playwright test
Triggers
pushto every branch. Many teams only run onmainand on pull requests. This repository runs on every branch because the exercise branches should produce a red run with traces you can download.pull_request: the result shows up as a check on the pull request, so a branch protection rule can block a merge on red tests.workflow_dispatch: a Run workflow button in the Actions tab, for when the site changed but the code did not.
The container
The job does not install browsers. It runs inside Microsoft’s Playwright image, which already contains the browsers and their system libraries for exactly one Playwright version. That has two advantages:
- Speed. No browser download on every run.
- The same machine as your visual baselines. In module 6 the baselines were made with
npm run test:visual, in this same image. CI renders fonts and anti-aliasing exactly like the machine the baselines came from.
The tag must match @playwright/test in package.json, which is why the version there is pinned exactly ("1.65.1", without ^). If they differ, the test runner looks for browser builds that the image does not have, and every test fails before it starts. When you upgrade Playwright, change both in the same commit.
--user 1001 makes the job run as an ordinary user instead of root. This follows Playwright’s own CI documentation: Firefox in particular refuses to run as root when the home folder belongs to a different user.
The alternative without a container is npx playwright install --with-deps as a step. It works, but it downloads the browsers on every run, and the result is no longer the image your baselines were made in.
The steps
npm ciinstalls exactly whatpackage-lock.jsonsays. Never usenpm installin CI: it may update the lock file and give you a different version than your laptop has.typecheckandlintcome first. They take seconds, and a type error or a floating promise (module 1) is caught before the browsers start.npx playwright testruns every project: unit, api, setup, with-cart, the three browsers and visual.
What CI changes
GitHub Actions sets the environment variable CI=true on every job. The config reacts to it in three places:
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
reporter: process.env.CI ? [["list"], ["blob"]] : [/* list, html, json, junit */],
forbidOnly: a forgottentest.onlyfails the run instead of quietly skipping the rest of the suite.retries: 2: a failed test is retried twice, and a test that passes on a retry is reported as flaky (lesson 35).- The
blobreporter: a raw format that can be merged later. Lesson 39 explains why.
The number of workers is not set, so Playwright uses half of the CPU cores. A standard GitHub runner has four cores, so it runs 2 workers, against 7 on the laptop the course was written on. Expect CI to take longer than a local run of the same tests.
Settings you might need
- A different site. The tests read
BASE_URL(viasrc/env.ts). To test a staging site, set a repository variable and pass it on:env: BASE_URL: ${{ vars.BASE_URL }}on the test step. - Secrets. This suite needs none, because the practice site’s login is public. For a real application, store passwords and tokens in Settings → Secrets and variables → Actions and pass them as
env:too. Never put them in the workflow file or in astorageStatefile you commit. - Timeouts.
timeout-minutes: 20stops a job that hangs, for example on a browser that never closes. GitHub’s default is six hours.
Exercise
- Fork the example repository and open its Actions tab. Enable workflows for the fork if GitHub asks.
- Push a commit that changes the first
test(intests/module-1/cart-maths.spec.tsintotest.only(. What happens in CI, and what would have happened locally? - Remove the
test.onlyagain.
Show suggested answer
In CI the run fails immediately, before any test runs. This is the real message (you can reproduce it locally with CI=1 npx playwright test --project=unit):
Error: item focused with '.only' is not allowed due to the 'forbidOnly' option in '../playwright.config.ts': "module-1/cart-maths.spec.ts subtotal adds price × quantity for every line"
at module-1/cart-maths.spec.ts:10
> 10 | test.only("subtotal adds price × quantity for every line", () => {
| ^
Locally, forbidOnly is false, and the run ends with 1 passed. Only that one test ran, and the rest of the suite was skipped without a warning. That is handy while you debug, and dangerous if it reaches main: the pipeline would be green while testing almost nothing.