Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Playwright Parallelism, Sharding, and Retries

Playwright Parallelism, Sharding, and Retries

This chapter introduces Playwright's parallel execution strategy, sharding, and failure retry mechanisms to help you efficiently run large-scale test suites.


Parallel execution

Playwright uses multiple worker processes by default to run tests in parallel, fully utilizing multi-core CPUs.

fullyParallel configuration

fullyParallel Controls whether tests within the same file run 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 When disabled, tests within the same file run serially, while different files run in parallel.

When fullyParallel: true When enabled, all tests run fully in parallel (including those within the same file).

workers count control

Example

export

default

defineConfig

(

{

 

// Use 4 workers

  workers

:

4

,

 

// Use default locally, fixed 2 on CI

  workers

:

process.

env

.

CI

?

2

:

undefined

,

 

// Single-thread mode (easier to debug)

  workers

:

1

,

}

)

;

Serial tests

Use test.describe.serial Force tests within the same group to run 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 serial tests because they break test isolation.

If tests need to run in order, consider combining multiple steps into a single test.

Local parallel control

Pass test.describe.configure You can control the parallel mode of an individual 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

Sharding distributes tests across multiple CI machines to shorten overall run time.

Usage



# Machine 1: Running piece 1/4

npx playwright test --shard=1/4



# Machine 2: Running plate 2/4

npx playwright test --shard=2/4



# Machine 3: Running plate 3/4

npx playwright test --shard=3/4



# Machine 4: Running plate 4/4

npx playwright test --shard=4/4

Merge sharded reports

After all machines finish running, merge the generated report files.



# Generate report for each machine (do not open automatically)

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

:

[

4

]

    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 automatically re-run failed tests, alleviating occasional flakiness.

Configure retries

Example

// File path: playwright.config.ts

export

default

defineConfig

(

{

 

// No retries locally (quick feedback), 2 retries on CI

  retries

:

process.

env

.

CI

?

2

:

0

,

}

)

;

Set retries for a single file or group

Example

// Set retries for the entire group

test.

describe

.

configure

(

{

retries

:

3

}

)

;

test.

describe

(

'Flaky feature (requires multiple retries)'

,

(

)

=>

{

  test

(

'Test A'

,

async

(

{

page

}

)

=>

{

/* ... */

}

)

;

  test

(

'Test B'

,

async

(

{

page

}

)

=>

{

/* ... */

}

)

;

}

)

;

Retry strategy

Strategy Description
CI environment retries No retries locally (quick feedback), 2-3 retries on CI
Trace collection together with trace: 'on-first-retry', record trace only on retries
Do not abuse retries Retries cannot hide real bugs. If a test frequently fails and only passes after a retry, the root cause should be fixed