Playwright Parallel Execution

A restaurant kitchen with five chefs cooking five different dishes at once serves a full dining room far faster than one chef cooking everything alone in sequence. Parallel execution gives a Playwright test suite that same multi-chef speed advantage.

Sequential Versus Parallel Runs

Sequential (one worker):
Test 1 -> Test 2 -> Test 3 -> Test 4   (total: slow)

Parallel (four workers):
Test 1
Test 2    all running together
Test 3    (total: much faster)
Test 4

Playwright's Default Parallel Behavior

Playwright runs different test files in parallel automatically, without any special configuration needed. Each file gets assigned to its own independent worker process, opening its own separate browser instance.

Setting a Specific Number of Workers

module.exports = {
  workers: 4,
};

This allows exactly four tests to run simultaneously, a number usually chosen to match the available processing power of the machine running the tests.

Letting Playwright Choose Automatically

module.exports = {
  workers: process.env.CI ? 2 : undefined,
};

Leaving the value undefined lets Playwright pick a sensible number based on available CPU cores, while a continuous integration server often benefits from a fixed, lower number to avoid overloading shared infrastructure.

Forcing Tests to Run in Order

test.describe.serial('Checkout Flow', () => {
  test('adds an item to the cart', async ({ page }) => {
    // test steps here
  });

  test('completes payment using the same cart', async ({ page }) => {
    // test steps here
  });
});

This forces both tests inside the group to run one after another in the exact written order, necessary when the second test genuinely depends on the result of the first.

A Real-World Example

A test suite with two hundred tests takes forty minutes to finish running one at a time. Configuring four parallel workers cuts that same run down to roughly ten minutes, letting a team get feedback on a code change much faster.

The Trade-Off With Shared Test Data

Parallel tests running at the same moment can accidentally interfere with each other if they share the same test account or database record. A test that empties a shared shopping cart might break a completely different test relying on that same cart still containing items.

Keeping Parallel Tests Independent

const test = base.test.extend({
  testUser: async ({}, use) => {
    const uniqueEmail = `user${Date.now()}${Math.random()}@example.com`;
    await use(uniqueEmail);
  },
});

Generating a fresh, unique piece of test data for every test run avoids conflicts between tests executing at the exact same time.

Quick Practice Task

Check how long your current test suite takes to run. Adjust the workers setting in playwright.config.js and compare the new run time.

Key Takeaways

  • Parallel execution runs multiple tests simultaneously to save time.
  • Playwright parallelizes test files automatically without extra setup.
  • The workers setting controls exactly how many tests run at once.
  • Independent test data prevents parallel tests from interfering with each other.

Leave a Comment

Your email address will not be published. Required fields are marked *