Rayrun

What is the method to enable parallel testing in Playwright?

Answer

To enable parallel testing in @playwright/test, you can use the test.describe.configure() method with the mode set to 'parallel'. Parallel tests run in separate worker processes and don't share any state or global variables. Each test executes all relevant hooks, including beforeAll and afterAll.

import { test } from '@playwright/test';

test.describe.configure({ mode: 'parallel' })('parallel tests', () => {
  test('first test', async ({ page }) => {
    await page.goto('https://ray.run/');
    // Your test code here
  });

  test('second test', async ({ page }) => {
    await page.goto('https://ray.run/');
    // Your test code here
  });
});

You can also enable fully-parallel mode for all tests by setting fullyParallel: true in the configuration file. This allows Playwright to shard a test suite for execution on multiple machines.

To disable parallelism and allow just a single worker at any time, set the workers: 1 option in the configuration file or pass --workers=1 to the command line when running your tests:

npx playwright test --workers=1

Each worker process has two IDs: a unique worker index starting with 1 and a parallel index between 0 and workers - 1. When a worker is restarted, the new worker process has the same parallelIndex but a new workerIndex. Access these indices through testInfo.workerIndex and testInfo.parallelIndex or from environment variables process.env.TEST_WORKER_INDEX and process.env.TEST_PARALLEL_INDEX.

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].