</>

Technology

Playwright

Difficulty

Advanced

Interview Question

What is test sharding in Playwright and how does it work?

Answer

Test Sharding in Playwright

Sharding distributes tests across multiple machines (CI agents) that run concurrently. Each shard handles a portion of the total test suite.

How Sharding Works

CODE
Total: 400 tests

Shard 1/4 → runs tests 1-100
Shard 2/4 → runs tests 101-200
Shard 3/4 → runs tests 201-300
Shard 4/4 → runs tests 301-400

All shards run simultaneously → 4x speedup!

Basic Sharding Command

Bash
# Machine 1 of 4
npx playwright test --shard=1/4

# Machine 2 of 4
npx playwright test --shard=2/4

# Machine 3 of 4
npx playwright test --shard=3/4

# Machine 4 of 4
npx playwright test --shard=4/4

GitHub Actions — Matrix Sharding

YAML
# .github/workflows/playwright.yml
name: Playwright Tests

jobs:
  test:
    timeout-minutes: 60
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        shardIndex: [1, 2, 3, 4]
        shardTotal: [4]

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Install dependencies
        run: npm ci

      - name: Install Playwright browsers
        run: npx playwright install --with-deps

      - name: Run tests (shard ${{ matrix.shardIndex }}/${{ matrix.shardTotal }})
        run: npx playwright test --shard=${{ matrix.shardIndex }}/${{ matrix.shardTotal }}

      # Upload each shard's results for merging
      - name: Upload shard report
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: playwright-report-${{ matrix.shardIndex }}
          path: playwright-report/
          retention-days: 30

Merging Shard Reports

To get one combined HTML report from all shards:

YAML
  merge-reports:
    needs: test
    if: always()
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with: { node-version: 20 }
      - run: npm ci

      - name: Download all shard reports
        uses: actions/download-artifact@v4
        with:
          path: all-blob-reports
          pattern: playwright-report-*

      - name: Merge reports
        run: npx playwright merge-reports --reporter html ./all-blob-reports

      - name: Upload combined report
        uses: actions/upload-artifact@v4
        with:
          name: playwright-report-combined
          path: playwright-report/

Shard + Workers (Maximum Speed)

Each shard also runs its tests in parallel via workers:

TypeScript
// playwright.config.ts
export default defineConfig({
  workers: 4,           // 4 parallel workers per shard
  fullyParallel: true,
});

With 4 shards × 4 workers = 16 tests running simultaneously!

When to Use Sharding

  • Test suite takes > 10 minutes on a single machine
  • CI pipeline needs to stay under a time limit
  • Running cross-browser tests (each browser can be a shard)

Follow AutomateQA

Related Topics