What is the role of fixtures in Playwright?
Fixtures are how Playwright Test prepares everything a test needs and tidies up after it. A test lists what it wants in its first argument, async ({ page, request }) => ..., and the runner builds only those things, passes them in, and disposes of them when the test ends. If you know beforeEach/afterEach from other frameworks, fixtures do the same job in a more reusable way.
Built-in fixtures
| Fixture | What you get | Scope |
|---|---|---|
page | A new tab in a fresh context | per test |
context | The fresh BrowserContext that owns page | per test |
request | An HTTP client for API testing | per test |
browser | The shared browser instance | per worker |
browserName | 'chromium', 'firefox' or 'webkit' | per worker |
Fixtures are lazy: a test that only asks for request never launches a page.
Writing your own fixture
Suppose many tests start on the course catalogue page. Instead of repeating the navigation in every test, wrap it in a page object and expose it as a fixture:
// fixtures.ts
import { test as base, expect, type Page } from '@playwright/test';
export class CataloguePage {
constructor(public page: Page) {}
async open() { await this.page.goto('/courses'); }
heading() { return this.page.getByRole('heading', { level: 1 }); }
card(name: string) { return this.page.getByRole('listitem').filter({ hasText: name }); }
}
type Fixtures = { catalogue: CataloguePage; city: string };
export const test = base.extend<Fixtures>({
city: ['Noida', { option: true }], // a value tests can override
catalogue: async ({ page, city }, use) => {
const catalogue = new CataloguePage(page);
await catalogue.open(); // setup
await page.getByLabel('City').selectOption(city);
await use(catalogue); // the test runs here
// teardown code would go here, after use()
},
});
export { expect };
// tests/catalogue.spec.ts
import { test, expect } from '../fixtures';
test('catalogue lists courses', async ({ catalogue }) => {
await expect(catalogue.heading()).toBeVisible();
});
test.describe('another city', () => {
test.use({ city: 'Pune' }); // override the option
test('shows the selected city', async ({ catalogue }) => {
await expect(catalogue.page.getByLabel('City')).toHaveValue('Pune');
});
});
Everything before await use(...) is setup, everything after it is teardown, and the teardown runs even when the test fails. In a check of this pattern, the log printed "setup Noida", the first test, "teardown", then "setup Pune" for the overridden group.
Worker-scoped fixtures
Some things are expensive and safe to share, such as an API token or a seeded database. Give them { scope: 'worker' } and they are created once per worker process and torn down when the worker finishes:
export const test = base.extend<{}, { apiToken: string }>({
apiToken: [async ({}, use) => {
const token = await getTokenSomehow(); // runs once per worker
await use(token);
}, { scope: 'worker' }],
});
With two tests in one worker, the token was set up before the first test and torn down only after the second.
Why fixtures beat hooks for shared setup
- On demand. Hooks run for every test in a file; a fixture runs only for tests that ask for it.
- Composable. A fixture can depend on other fixtures (
catalogueusespageandcity). - Reusable across files. Export one extended
testand import it everywhere. - Typed. In TypeScript, the editor knows exactly what
catalogueis.
Hooks still have their place; compare them in beforeAll and afterAll hooks. Fixture options live naturally in the config file.
Common mistakes
- Forgetting
await use(...): the test never starts and times out. - Importing
testfrom@playwright/testinstead of yourfixtures.ts: custom fixtures are then "unknown". - Using a test-scoped fixture such as
pageinside a worker-scoped fixture: not allowed, because a worker outlives each test. - Auto fixtures (
{ auto: true }) run for every test even if unused; keep them light.
Want to master Playwright hands-on?
Join Uncodemy's Playwright Automation Testing Course in Noida — Dedicated Placement Support.
Explore the Course →