33. Scaling Execution: Multi-Worker Parallelism & Device Matrix

Worker Architecture & Test Isolation

- Playwright starts multiple Worker OS processes (e.g. 4 workers on a 4-core CPU). - Each test file runs inside a worker. - Tests within the same file run sequentially by default. - To run tests within a file in parallel: test.describe.configure({ mode: 'parallel' });.

---

Cross-Browser Matrix Configuration (playwright.config.ts)

import { defineConfig, devices } from "@playwright/test";

export default defineConfig({
workers: process.env.CI ? 2 : undefined, // Auto-detect CPUs locally
projects: [
{
name: "Desktop Chrome",
use: { ...devices["Desktop Chrome"] },
},
{
name: "Desktop Firefox",
use: { ...devices["Desktop Firefox"] },
},
{
name: "Desktop Safari",
use: { ...devices["Desktop Safari"] },
},
{
name: "Mobile iPhone 14",
use: { ...devices["iPhone 14"] },
},
],
});

---

Serial Execution for State-Dependent Workflows

When a small set of tests MUST run in order:
test.describe.serial("Order Processing Pipeline", () => {
  test("create order", async () => { /* ... */ });
  test("process payment", async () => { /* ... */ });
  test("ship order", async () => { /* ... */ });
});

Task

  • Implement calculateOptimalWorkerCount:
  • 1. Accept cpuCores (number) and isCi (boolean).
  • 2. Return 2 for CI; return max(1, cpuCores - 1) for local workstations.
πŸ’‘Common doubts
Did you like the problem?
Lesson 33 of 36
TypeScript
Loading code editor…
Next