Playwright Parallel, Sharding and Retry | Beginner Tutorial
Source: https://example.com/playwright/playwright-parallel-sharding.html
This chapter introduces Playwright's parallel execution strategy, sharding running and failure retry mechanism, helping you efficiently run large-scale test suites.
Parallel Execution
Playwright uses multiple Worker processes to run tests in parallel by default, making full use of multi-core CPUs.
fullyParallel Configuration
fullyParallel controls whether tests within the same file are executed in parallel.
Example
// File path: playwright.config.ts
export default defineConfig({
fullyParallel:true,// Tests within the same file also run in parallel
});
When fullyParallel: false, tests within the same file are executed serially, while different files are executed in parallel.
When fullyParallel: true, all tests are fully parallel (including those within the same file).
Workers Quantity Control
Example
export default defineConfig({
// Use 4 Workers
workers:4,
// Use default value locally, use fixed 2 in CI
workers: process.env.CI?2:undefined,
// Single-threaded mode (convenient for debugging)
workers:1,
});
Serial Testing
Use test.describe.serial to force tests within the same group to execute serially.
Example
// Serial executionββeach test depends on the previous test's result
test.describe.serial('User Registration to Login',()=>{
test('Step 1: Register', async ({ page })=>{/* ... */});
test('Step 2: Verify Email', async ({ page })=>{/* ... */});
test('Step 3: Login', async ({ page })=>{/* ... */});
});
Try to avoid using serial testing because it breaks test isolation.
If tests need to execute in order, consider combining multiple steps into one test.
Local Parallel Control
Through test.describe.configure, you can control the parallel mode of a single group.
Example
test.describe('A Group of Tests',()=>{
test.describe.configure({ mode:'serial'});// Force serial
// test.describe.configure({ mode: 'parallel' }); // Force parallel
test('A', async ()=>{});
test('B', async ()=>{});
});
Sharding Running
Sharding distributes tests across multiple CI machines to execute, shortening the overall running time.
Usage
# Machine 1: Run shard 1/4
npx playwright test --shard=1/4
# Machine 2: Run shard 2/4
npx playwright test --shard=2/4
# Machine 3: Run shard 3/4
npx playwright test --shard=3/4
# Machine 4: Run shard 4/4
npx playwright test --shard=4/4
Merging Shard Reports
After all machines finish running, merge the generated report files.
# Each machine generates a report (not automatically open)
npx playwright test --shard=1/4 --reporter=blob
# Merge all blob reports
npx playwright merge-reports --reporter=html ./all-blob-reports
Sharding Configuration in GitHub Actions
Example
# File path: .github/workflows/playwright.yml
jobs:
test:
strategy:
fail-fast: false
matrix:
shardIndex: [1, 2, 3, 4]
shardTotal:
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- run: npm ci
- run: npx playwright test --shard=${{ matrix.shardIndex }}/${{ matrix.shardTotal }}
Retry Mechanism
Retries let failed tests run again automatically, alleviating occasional unstable factors.
Configure Retry
Example
// File path: playwright.config.ts
export default defineConfig({
// No retry locally (quick feedback), retry 2 times on CI
retries: process.env.CI?2:0,
});
YouTip