@pw-studio/cli
v0.1.0-20261121171524-1e33f5
Published
Playwright Studio CLI (pws) and Playwright reporter
Readme
@pw-studio/cli
Duration-aware sharding for Playwright. pws test wraps playwright test: it records how long
every test file takes and splits the files across shards so each shard finishes at about the same
time, instead of Playwright's alphabetical split.
Getting started
Add the CLI next to
@playwright/test(1.50 or newer):npm install --save-dev @pw-studio/cliInstall the Playwright Studio GitHub App on your repository, copy the project secret from the dashboard and store it as a CI secret named
PWS_SECRET.In CI, replace
playwright testwithpws testand pass the secret (see GitHub Actions). Commit and push: this first run has no history yet, so it splits files evenly and records how long each one takes.To see duration-aware sharding in action, push another commit. Re-running the first workflow is not enough: a run keeps the plan it started with (see Plans), so only a new run uses the timings recorded in step 3. An empty commit works:
git commit --allow-empty -m "Shard with recorded timings" && git push
Usage
pws test # runs everything, records timings in CI
pws test --shard=1/4 # runs the files the studio assigned to shard 1 of 4
pws test tests/checkout -- --grep smoke --workers 2Test file filters go before --; any other Playwright argument goes after it and is passed on
unchanged. Your configured reporters are kept. pws test --help lists every flag.
On a pull request, when a shard runs tests the PR adds or changes, pws test records a trace, a
video and screenshots for that shard whatever your config's trace, video and screenshot say,
so the report linked from the PR comment can show them even though the tests passed. Only the new
and changed tests' recordings are uploaded. --pws-upload=always or never turns this off.
A report over the studio's 1 GiB limit per shard is uploaded without the attachments of passing tests, so it still shows every result and what failing and flaky tests recorded.
GitHub Actions
jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
shard: [1, 2, 3, 4]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
- run: npm ci
- run: npx playwright install --with-deps
- run: npx pws test --shard=${{ matrix.shard }}/${{ strategy.job-total }}
env:
PWS_SECRET: ${{ secrets.PWS_SECRET }}Plans
The first shard of a run to start computes the plan and every other shard of that run gets the same one. The plan is fixed from then on: re-running the workflow ("Re-run failed jobs" or "Re-run all jobs") reuses it, even if timings were recorded since. Push a new commit to get a fresh plan.
The run id is taken from GitHub Actions, GitLab CI, Buildkite or CircleCI automatically; elsewhere
set PWS_RUN_ID to a value shared by every shard of the run.
Timings are kept per kind of machine, identified as <platform>-<arch>-<cpus>cpu (e.g.
linux-x64-16cpu), so a fast runner's durations never skew a slow runner's plan. Local runs (no
run id) record nothing, and --shard needs a run id.
Settings
| Setting | Flag | Env | Default |
| -------------- | ----------- | ----------------- | ----------------------------------- |
| Project secret | | PWS_SECRET | required to shard and record |
| Server | --pws-url | PWS_URL | https://api.pw.studio |
| Run id | | PWS_RUN_ID | detected from CI, else none (local) |
| CI attempt | | PWS_RUN_ATTEMPT | GITHUB_RUN_ATTEMPT, else 1 |
If the studio cannot be reached while sharding, pws exits with code 1 rather than risk skipping or
duplicating tests across shards.
Visual regression
Take screenshots in your tests and Playwright Studio compares each one with what the pull request's base branch last produced. Nothing is committed to your repository: there are no snapshot files to update.
import { test } from "@playwright/test";
import { screenshot } from "@pw-studio/cli/test";
test("checkout", async ({ page }) => {
await page.goto("/checkout");
await screenshot(page, "checkout");
await screenshot(page, "checkout, whole page", {
fullPage: true,
mask: [page.locator(".clock"), page.getByTestId("avatar")],
});
await screenshot(page.getByRole("dialog"), "confirm dialog");
});screenshot(target, name, options?) takes a page or a locator (for one element). The name
identifies the screenshot within its test. options are Playwright's own
screenshot options except path,
type and quality: the ones you will reach for are fullPage, mask (elements painted over
with a solid box), maskColor, clip and style (CSS applied while the screenshot is taken, to
hide or freeze something).
To keep screenshots the same from run to run, the helper freezes CSS animations, hides the text
caret, uses CSS pixels, waits for fonts, and retakes the screenshot until two in a row are
identical. If the page keeps changing for 5 seconds (options.timeout), the test fails and says
so: mask or freeze what moves. That is the only way a screenshot fails a test. A visual change
never does.
A screenshot may have up to 32 million pixels (1280 by 25000, say) and its name is one line of at most 200 characters; one over the size limit is skipped with a warning.
In CI the screenshots are recorded with the run's results, whatever the upload policy, and only images the studio does not have yet are uploaded. Then:
- The pull request comment says how many screenshots changed, are new or were removed, and links to the run's visual page.
- The visual page shows each screenshot next to its baseline: side by side, with a swipe divider, as an onion skin, or as a highlighted diff.
- The baseline of a test is what it produced the last time it passed on the base branch, on the same operating system and architecture. Merging the pull request makes its screenshots the new baseline. Runs on a branch outside a pull request are compared with the branch's previous run.
Locally (no run id) the screenshots are only attached to the test, so they show up in Playwright's own report.
Reporter only
To record timings in CI without the wrapper, add the reporter to playwright.config.ts:
reporter: [["list"], ["@pw-studio/cli/reporter"]],License
MIT
