Playwright + TS · Lesson 33 of 36

0/36 done

Module 33: Parallel & Cross-Browser Testing

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

18 minlesson

Try it yourself

Implement calculateOptimalWorkerCount:

  1. Accept cpuCores (number) and isCi (boolean).
  2. Return 2 for CI; return max(1, cpuCores - 1) for local workstations.

The editor has a worked example. Read it, tweak it, then press Mark as complete.

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)

typescript
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:

typescript
test.describe.serial("Order Processing Pipeline", () => {
  test("create order", async () => { /* ... */ });
  test("process payment", async () => { /* ... */ });
  test("ship order", async () => { /* ... */ });
});

Key Takeaways & Walkthrough Summary

To give fast feedback to developers, large test suites need to run in parallel. In this lesson, we will explore Playwright's multi-worker process architecture, test isolation, serial test modes, and cross-browser testing matrices across Chromium, Firefox, WebKit, and mobile viewports. Let's scale our test execution!

Part of the Playwright + TS course (Playwright with TypeScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Do workers share memory or global variables?

No. Each worker is an independent Node.js process. Global in-memory variables set in one test file will NOT exist in another worker.

How does Playwright emulate mobile devices?

Playwright configures the viewport, deviceScaleFactor, userAgent, and touch screen emulation using the Chromium/WebKit engine.

Finished reading?

Review the worked example in the editor, then mark the lesson complete.

TypeScript
Skip