πŸ’‘ If you like this website, please share it with your friends and network! πŸš€
Back to All Questions
Question 7 of 100
Execution
Intermediate

Parallel Execution in Playwright?

The Answer

Playwright runs tests in parallel by default using worker processes. Each worker handles one test at a time in its own browser. You control parallelism via `workers` config and `fullyParallel` flag.

Deep Dive Explanation

Workers are isolated OS processes β€” a crash in one worker doesn't affect others. Playwright reuses browser instances within a worker across tests (for performance) but creates a fresh BrowserContext per test (for isolation). The sweet spot for workers is usually equal to the number of CPU cores.

example.spec.ts
// playwright.config.ts
export default defineConfig({
  fullyParallel: true,  // All tests across ALL files run in parallel
  workers: process.env.CI ? 4 : '50%', // 4 workers in CI, half CPUs locally
});

// Force parallel within a single file
test.describe.configure({ mode: 'parallel' });

// Force serial (one at a time) within a describe
test.describe.serial('checkout flow', () => {
  test('step 1: add to cart', async ({ page }) => { /* ... */ });
  test('step 2: checkout', async ({ page }) => { /* ... */ });
});

// CLI: Run with specific worker count
// npx playwright test --workers=8

// Shard across machines (divide suite into N parts)
// npx playwright test --shard=1/4  (machine 1 of 4)