The SDET Playbook

← All questions

How do you write, extend, and configure custom worker-scoped and test-scoped fixtures in Playwright?

Asked Sep 28, 2026Viewed 0 times

1 Answer

Sign in to answer and to vote.

  • 0
    The SDET PlaybookSep 28, 2026

    A fixture sets something up, hands it to the test with use, and tears it down afterward. Test-scoped fixtures (the default) run once per test. Worker-scoped fixtures run once per worker process and are shared by every test that worker runs, which suits expensive setup such as creating an account or opening a connection pool. test.extend takes two type parameters, the test fixtures and then the worker fixtures:

    import { test as base } from '@playwright/test';
    import { UserPage } from './UserPage';
    import { createAccount, deleteAccount, type Account } from './accounts';
    
    type TestFixtures = { userPage: UserPage };
    type WorkerFixtures = { account: Account };
    
    export const test = base.extend<TestFixtures, WorkerFixtures>({
      // Once per worker: each parallel worker gets its own account.
      account: [async ({}, use, workerInfo) => {
        const account = await createAccount(`worker-${workerInfo.workerIndex}`);
        await use(account);
        await deleteAccount(account);
      }, { scope: 'worker' }],
    
      // Once per test: a fresh page object that can use the worker's account.
      userPage: async ({ page, account }, use) => {
        const userPage = new UserPage(page);
        await userPage.signIn(account);
        await use(userPage);
      },
    });
    

    A test-scoped fixture can depend on a worker-scoped one, but not the other way round. Add { auto: true } for a fixture every test should get without asking for it, and override a built-in fixture (for example page) the same way you define a new one.

    Sources: Playwright fixtures