How do I create and manage worker fixtures in Playwright Test?


Question: How to properly use a worker fixture

To use a worker fixture in @playwright/test, set up services and run servers for each worker process. Worker fixtures are similar to test fixtures but are set up for each worker process instead of individual test runs.

Create a shared account fixture for all tests in the same worker by overriding the page fixture. Use workerInfo.workerIndex to generate unique accounts for any test or fixture. Pass scope: 'worker' to set up the fixture once per worker.

Here's an example:

import { test, expect, WorkerInfo } from '@playwright/test';

  baseURL: 'https://ray.run',

test('worker fixture example', async ({ page }) => {
  // Your test code here

test.extend<{ sharedAccount: string }>({
  sharedAccount: [async ({ workerInfo }: { workerInfo: WorkerInfo }, use) => {
    const account = `account-${workerInfo.workerIndex}`;
    // Set up the shared account
    await use(account);
  }, { scope: 'worker' }],

Remember that autoWorkerFixture is set up for the beforeAll hook, but autoTestFixture is not. Page and autoTestFixture are set up and torn down for each test as test-scoped fixtures, while unusedFixture is never set up if not used by any tests/hooks.

In summary, use worker fixtures to set up services and run servers per worker process. Create shared account fixtures with workerInfo.workerIndex and configure your setup with appropriate scopes for a consistent environment.

Thank you!
Was this helpful?
Still have questions?

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

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 luc@ray.run.