QA and Test Automation · Lab 25 of 35

Shard Playwright Tests in CI and Merge the Reports: Hands-On Lab

Split ShopRung's Playwright suite across 3 machines and merge the results into one HTML report: why sharding by file left one shard empty, fullyParallel, blob reports, merge-reports, a GitHub Actions matrix with a merge job, and an honest look at how much faster it really was.

⏱ 60 minIntermediate💸 Free6 steps

Playwright Test 1.63TypeScriptGitHub ActionsShopRung

Overview video

1 / 6

0 of 6 steps done

What you will build

The Playwright suite from lab 13, sharded the way large teams run UI tests:

  • A config that shards by test (fullyParallel), keeps one worker per shard, and writes a blob report in CI.
  • Commands that run 3 shards locally exactly as CI would, and merge-reports that turns their blobs into one HTML report and one JUnit file.
  • .github/workflows/ui-sharded.yml: a matrix of 3 shard jobs, each with its own ShopRung, and a merge-reports job that runs even when a shard fails.
  • Real measurements: what sharding by file did (2, 4 and 0 tests), what fullyParallel did (2, 2, 2), and how much time 3 shards saved on a small suite.

Before you start

Setup

S1Start ShopRung and open the lab 13 project

Start ShopRung as in lab 1 with npm start. In another terminal, go to the Playwright project from lab 13 and check it passes.

Run
cd shoprung-e2e
npx playwright test
You should seeyour text will differ: the model writes it
  7 passed (4.7s)
1

Measure, then try shards as they are

See how Playwright splits the suite when it shards by file.

--shard=i/n runs part i of n. Run the three parts one after another and look at which tests each one gets. ShopRung's suite has the login setup plus 6 tests in three files: cart.spec.ts (2 tests), checkout.spec.ts (1) and search.spec.ts (3).

Lab 13 set fullyParallel: false, so Playwright shards by file: whole files are dealt out, and here shard 1 got the cart file, shard 2 got checkout and search (4 tests), and shard 3 got nothing at all. It printed nothing and exited 0: in CI, that is a machine started for no reason, and nobody notices.

Run
npx playwright test --shard=1/3
npx playwright test --shard=2/3
npx playwright test --shard=3/3
You should seeyour text will differ: the model writes it
-- shard 1/3: setup + cart.spec.ts (2 tests)
  3 passed
-- shard 2/3: setup + checkout.spec.ts + search.spec.ts (4 tests)
  5 passed
-- shard 3/3:
  (no output, exit code 0)
2

Shard by test, report as blobs

Balanced shards, one test at a time per shard, a mergeable report in CI.

Edit playwright.config.ts. fullyParallel: true lets Playwright split individual tests across shards instead of whole files. Keep workers: 1: inside one shard, tests still share that shard's ShopRung and reset it before each test, so they must not overlap. In CI, every shard will have its own ShopRung.

The reporter line picks blob when the CI environment variable is set. A blob report is a zip of raw results meant for merging, not for reading. On your laptop you keep the list and HTML reporters.

playwright.config.ts 5 new or changed lines
import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  testDir: './tests',
  // Shards split by TEST, not by file. Each shard still runs one test at a time (workers: 1),
  // because a shard's tests share that shard's ShopRung and reset it.
  fullyParallel: true,
  workers: 1,
  retries: process.env.CI ? 2 : 0,
  // In CI each shard writes a compact "blob" report; a final job merges them into one HTML report.
  reporter: process.env.CI ? 'blob' : [['list'], ['html', { open: 'never' }]],
  use: {
    baseURL: process.env.BASE_URL ?? 'http://localhost:8080',
    trace: 'on-first-retry',
    screenshot: 'only-on-failure',
  },
  projects: [
    { name: 'setup', testMatch: /.*\.setup\.ts/ },
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'], storageState: 'playwright/.auth/alice.json' },
      dependencies: ['setup'],
    },
  ],
});
  1. Shard by test. With workers: 1 it still runs tests one by one inside each shard.line 7
  2. GitHub sets CI=true; locally you will set CI=1 to imitate it.line 11
  3. Every shard runs the login setup first, because every shard needs a logged-in state.line 22
Run
npx playwright test --shard=1/3
npx playwright test --shard=2/3
npx playwright test --shard=3/3
You should seeyour text will differ: the model writes it
-- shard 1/3: setup + 2 cart tests        3 passed
-- shard 2/3: setup + checkout + 1 search 3 passed
-- shard 3/3: setup + 2 search tests      3 passed
3

Run three shards like CI

Three blob reports, collected in one folder.

Set CI=1 so each run writes blob-report/report-<shard>.zip. Playwright empties blob-report at the start of every run, so copy each shard's zip into all-blob-reports before running the next one. In CI, each shard runs on its own machine and uploads its zip instead.

Run
mkdir -p all-blob-reports
for i in 1 2 3; do
  CI=1 npx playwright test --shard=$i/3
  cp blob-report/*.zip all-blob-reports/
done
ls all-blob-reports
You should see
report-1.zip  report-2.zip  report-3.zip
Stuck on this step?
Only one zip in all-blob-reports.
Copy after each shard; the next run deletes blob-report first. Check the cp line is inside the loop.
4

Merge into one report

One HTML report and one JUnit file from three shards.

merge-reports reads every blob in the folder and writes one report with any reporter you name. HTML is for people: open it and you see all results together, with traces and screenshots from whichever shard produced them. JUnit XML is for CI tools and run summaries; PLAYWRIGHT_JUNIT_OUTPUT_NAME sends it to a file instead of the terminal.

The merged results count the setup three times, once per shard, plus the 6 tests: 9 results.

Run
npx playwright merge-reports --reporter html ./all-blob-reports
PLAYWRIGHT_JUNIT_OUTPUT_NAME=results.xml npx playwright merge-reports --reporter junit ./all-blob-reports
npx playwright show-report
You should seeyour text will differ: the model writes it
merging reports from .../all-blob-reports
extracting: all-blob-reports/report-1.zip
extracting: all-blob-reports/report-2.zip
extracting: all-blob-reports/report-3.zip
...
finished building report

results.xml: <testsuites ... tests="9" failures="0" ...>
5

The sharded workflow

Three shard jobs in parallel, then one merge job.

In your repository from lab 23 (with shoprung/ and e2e/), copy the new playwright.config.ts into e2e/ and create .github/workflows/ui-sharded.yml. The ui job has a matrix of shardIndex 1 to 3; each copy starts its own ShopRung, runs its shard, and uploads its blob under a unique name.

The merge-reports job needs: ui, so it waits for all three, and runs if: ${{ !cancelled() }}, so it still runs when a shard fails, which is when you need the report most. It downloads every blob-report-* artifact into one folder (merge-multiple: true), merges, and uploads the HTML report and JUnit file.

.github/workflows/ui-sharded.yml
name: ui-sharded

on:
  pull_request:
  push:
    branches: [main]
  workflow_dispatch:

permissions:
  contents: read

jobs:
  ui:
    runs-on: ubuntu-latest
    timeout-minutes: 20
    strategy:
      fail-fast: false
      matrix:
        shardIndex: [1, 2, 3]
        shardTotal: [3]
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
          cache-dependency-path: e2e/package-lock.json

      - name: Start ShopRung (each shard gets its own)
        working-directory: shoprung
        run: |
          npm start > shoprung.log 2>&1 &
          npx --yes wait-on@8 http-get://localhost:8080/api/health --timeout 60000

      - name: Install Playwright and Chromium
        working-directory: e2e
        run: |
          npm ci
          npx playwright install --with-deps chromium

      - name: Run shard ${{ matrix.shardIndex }} of ${{ matrix.shardTotal }}
        working-directory: e2e
        run: npx playwright test --shard=${{ matrix.shardIndex }}/${{ matrix.shardTotal }}

      - name: Keep this shard's blob report
        if: ${{ !cancelled() }}
        uses: actions/upload-artifact@v4
        with:
          name: blob-report-${{ matrix.shardIndex }}
          path: e2e/blob-report
          retention-days: 1

  merge-reports:
    needs: ui
    if: ${{ !cancelled() }}
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
          cache-dependency-path: e2e/package-lock.json

      - name: Install Playwright (no browser needed to merge)
        working-directory: e2e
        run: npm ci

      - name: Download every shard's blob report
        uses: actions/download-artifact@v4
        with:
          path: e2e/all-blob-reports
          pattern: blob-report-*
          merge-multiple: true

      - name: Merge into one HTML report and one JUnit file
        working-directory: e2e
        run: |
          npx playwright merge-reports --reporter html ./all-blob-reports
          PLAYWRIGHT_JUNIT_OUTPUT_NAME=results.xml npx playwright merge-reports --reporter junit ./all-blob-reports

      - name: Keep the merged report
        uses: actions/upload-artifact@v4
        with:
          name: playwright-report
          path: |
            e2e/playwright-report/
            e2e/results.xml
          retention-days: 14
  1. Three jobs: ui (1, 3), ui (2, 3), ui (3, 3).line 19
  2. Shards never share data: no reset from one shard can break another.line 30
  3. Artifact names must be unique per job.line 50
  4. Blobs are only needed by the merge job.line 52
  5. Merge even when a shard failed; skip only if the run was cancelled.line 47
  6. All three zips land in one folder, as merge-reports expects.line 77
Run
actionlint
git add e2e/playwright.config.ts .github/workflows/ui-sharded.yml
git commit -m "CI: shard UI tests" && git push
You should seeyour text will differ: the model writes it
ui-sharded:
  ui (1, 3) ✓   ui (2, 3) ✓   ui (3, 3) ✓
  merge-reports ✓   Artifacts: playwright-report
6

Was it faster?

Measure, and learn when sharding pays.

We timed the suite on one machine: the full run took about 4.7 s, and each of the 3 shards about 3.2 s. Three machines saved only about a third, because every shard pays the same fixed costs again: starting Playwright, launching the browser and logging in. In CI, each shard also pays for checkout, installs and starting ShopRung, often a minute or more.

So shard when the tests dominate: a suite of 300 UI tests at 10 seconds each takes 50 minutes on one machine and about 10 on five. Choose the shard count by measuring, keep shards balanced (fullyParallel, similar test lengths), and watch the slowest shard: the run finishes only when it does.

Run
time npx playwright test
time npx playwright test --shard=1/3
You should seeyour text will differ: the model writes it
full run      4.7 s   7 passed
shard 1/3     3.2 s   3 passed
shard 2/3     3.4 s   3 passed
shard 3/3     3.0 s   3 passed

Check it works

Switch on ShopRung's coupon-boundary bug and run the three shards with CI=1 as in step 3. Shard 1 exits with code 1 (it holds the SAVE10 test); shards 2 and 3 exit 0. Merge the blobs: the merged report shows 1 failed, 8 passed, and the failed test appears with its two retries and the trace from the first retry, although it ran on another "machine" than the others.

Turn the bug off. In CI, the same happens per job: one red shard, a green merge-reports job, and one report artifact with everything in it.

Test
curl -X POST http://localhost:8080/api/test/bugs -H "Content-Type: application/json" -d '{"enabled":["coupon-boundary"]}'
# run the 3 shards as in step 3, then:
npx playwright merge-reports --reporter list ./all-blob-reports
You should see
Running 9 tests using 3 workers
  ✓  1 [setup] › tests/auth.setup.ts:4:6 › log in as alice
  ✘  2 [chromium] › tests/cart.spec.ts:3:5 › SAVE10 at exactly $50.00 gives a total of $49.99
  ✘  3 [chromium] › tests/cart.spec.ts:3:5 › SAVE10 at exactly $50.00 gives a total of $49.99 (retry #1)
  ✘  4 [chromium] › tests/cart.spec.ts:3:5 › SAVE10 at exactly $50.00 gives a total of $49.99 (retry #2)
  ✓  5 [chromium] › tests/cart.spec.ts:16:5 › shipping is free from $75.00
  ...
  1 failed
  8 passed

Troubleshooting

One shard runs no tests and exits 0.

You have more shards than files with fullyParallel: false. Turn on fullyParallel or use fewer shards.

Tests fail only when sharded.

Tests depend on each other or on order. Each test must create its own state; the shard boundary moves tests between machines.

merge-reports: "No report files found".

The folder must contain the report-*.zip files directly. In CI, use merge-multiple: true so artifacts are not put in subfolders.

The merge job is skipped when a shard fails.

Add if: ${{ !cancelled() }} (or if: always()) to the merge job; needs alone skips it on failure.

Artifact upload fails with a name conflict.

Include the shard index in the artifact name: blob-report-${{ matrix.shardIndex }}.

What changes in production

  • Large UI suites run as 4 to 20 shards with blob reports and a merge job; the merged HTML report is what people open.
  • Each shard gets its own isolated environment or data, so shards never interfere.
  • Teams track shard durations and rebalance; Playwright's --last-failed and test lists help rerun only what failed.
  • For pytest, the same ideas use pytest-xdist (workers on one machine) and a sharding plugin or test list per CI job, with JUnit files merged at the end.

Interview angle

“Your UI suite takes 45 minutes in CI. What do you do?”

First push checks down the pyramid so fewer tests need a browser. Then shard: Playwright with fullyParallel so shards split by test, a CI matrix of shards where each job has its own environment and data, blob reports uploaded per shard, and a merge job that runs even on failure and publishes one HTML report and a JUnit file. Measure the shard durations: each shard has fixed setup cost, so pick the count where the slowest shard is short without wasting machines, and fix any test that depends on order or shared state.

Clean up

Practice

These run in your browser and check your answer.

Split tests into shards

Playwright gives each shard a contiguous slice of the tests, and when they do not divide evenly the first shards get one extra. Write shard(tests, index, total) (index starts at 1) that returns the tests for that shard. 7 tests in 3 shards are 3, 2 and 2.

Exercise
def shard(tests, index, total):
    size = len(tests) // total
    start = (index - 1) * size
    return tests[start:start + size]
Hint

Use divmod: size, extra = divmod(len(tests), total). Shard i starts at (i-1)*size + min(i-1, extra).

The full project

Every file from this lab, as it is after the last step.

Download TypeScript project (.zip)