Playwright Test fixtures let you define a resource’s setup, make it available to the tests that need it, and clean it up in one place. A test requests fixtures by name in its function arguments; Playwright initializes the required non-automatic fixtures, supplies their values, and runs their teardown after use. Choosing the right fixture scope—and making dependencies and cleanup explicit—is what makes this pattern useful for maintainable end-to-end tests.
How Playwright Test fixtures work
A fixture packages a test resource’s lifecycle: setup happens before await use(value), the test receives the value at that point, and teardown runs after the test is finished with it. Built-in fixtures such as page and browser are familiar examples. Custom fixtures use the same model.
As an Amazon Associate I earn from qualifying purchases.
Playwright analyzes fixture parameters requested by tests, hooks, and other fixtures. It sets up the required fixtures and makes their values available. Ordinary fixtures are on demand, so an unused fixture is not initialized. The official Playwright fixtures guide describes this lifecycle and its execution order.
Free tools Windows power users keep installed
One-click scans. No signup required.
Create a custom fixture
Call test.extend() on a base test object, then import the extended test in tests that need the fixture. This TypeScript example supplies a page object after navigating to the app’s base URL:
#1 Best Overall
import { test as base } from '@playwright/test';
import { DashboardPage } from './dashboard-page';
type Fixtures = {
dashboard: DashboardPage;
};
export const test = base.extend<Fixtures>({
dashboard: async ({ page, baseURL }, use) => {
const dashboard = new DashboardPage(page);
await dashboard.goto(baseURL!);
await use(dashboard);
},
});
The fixture depends on the built-in page and baseURL fixtures, so Playwright provides them before it runs this setup. The test’s function argument requests dashboard; the page object is created and navigated before the test body starts.
Keep cleanup beside setup
Put cleanup after await use(value) when the fixture creates data or another resource that must be removed:
testData: async ({}, use) => {
const record = await createTestRecord();
try {
await use(record);
} finally {
await deleteTestRecord(record.id);
}
},
Use cleanup that matches what setup actually created. If setup fails before the fixture reaches use, or an external service is unavailable during teardown, the cleanup path may not complete as expected; avoid relying on fixture teardown as the only safeguard for important external state. The fixture guide explains the lifecycle, but cleanup still depends on the resource and service involved.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallCrashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteRank #2
Choose test or worker scope
Custom fixtures are test-scoped by default. A test-scoped fixture is created for each test that requests it and torn down after that test. A worker-scoped fixture is created once for a worker process and torn down when that worker shuts down. Scope determines whether a resource is fresh per test or intentionally shared, not whether your application’s state is automatically isolated.
| Scope | Lifecycle and isolation | Good fit | Watch for |
|---|---|---|---|
| Test (default) | Set up for a test that requests it; torn down after that test. | Mutable test data, per-test helpers, or state that must not leak between tests. | Repeated setup may cost more when the resource is expensive to create. |
| Worker | Set up for a worker process; torn down when that worker shuts down. | An expensive service or account deliberately reused by tests in the same worker. | Tests share mutable state unless you provide explicit isolation or reset behavior. |
For example, the Playwright guide demonstrates deriving an account identity from workerInfo.workerIndex in a worker-scoped fixture, then using a test-scoped page for each test. That is a pattern, not a guarantee that your application supports concurrent accounts or that the same account-creation approach fits your system.
Playwright’s built-in browser fixtures illustrate a related distinction: the browser process is reused within a worker, while each test gets a separate BrowserContext and Page. This provides test-level browser-state isolation while avoiding a new browser process for every test. It does not isolate external databases, accounts, or other application services. See the fixture API reference for built-in fixture behavior.
How fixture dependencies control lifecycle
A fixture can request another fixture in its parameter list. If fixture A depends on fixture B, Playwright sets up B first, then A; teardown reverses that order, so A is torn down before B. Put the lower-level resource provider in the dependency position and the higher-level helper in the dependent position. This ensures a helper is not torn down after the resource it needs has already been removed.
For instance, a page-object fixture can depend on page, and a seeded-data fixture can depend on a database connection fixture. Dependencies also let a test request a higher-level fixture without naming every underlying resource. The fixtures guide describes dependencies, lazy setup, and execution order.
When to use automatic fixtures
Set auto: true when a fixture should run for every test or worker at its configured scope, even if no test names it. This can suit cross-cutting diagnostics that should accompany every test. Otherwise, prefer explicit fixture requests: a test’s arguments then show which extra behavior it needs, rather than hiding setup from the test body.
Rank #4
Automatic fixtures participate in hook ordering. In Playwright’s documented example, automatic worker fixtures run before beforeAll, while automatic test fixtures run before test and beforeEach work. Ordinary fixtures are initialized when the relevant hook or test needs them. Test-scoped teardown follows the test’s hooks; worker-scoped teardown occurs at worker shutdown. Worker restarts and failures can change what you observe across a suite, so do not assume there is one worker or one setup event for the entire run.
Fixture timeouts and slow setup
Fixture setup and teardown generally count toward the test timeout. A slow fixture can therefore cause a timeout even when the test’s interactions and assertions are quick. Playwright allows a timeout to be set on an individual fixture; worker-scoped fixtures also have their own timeout, which defaults to the test timeout and can be configured. Diagnose whether time is being spent in fixture startup, the test body, or teardown before changing the timeout. See Playwright’s timeout documentation for the relevant timeout behavior.
Use built-in fixtures and overrides deliberately
Core built-ins include browser, context, page, and request. The browser is shared within a worker; contexts and pages are isolated per test, and request provides an isolated API request context for a test. Use a built-in when its lifecycle matches the resource your test needs.
An override can wrap an existing fixture or replace its behavior. For example, a page override can depend on the existing page and add navigation, while a storageState override can provide a different authentication state. These are not the same operation: one adds behavior around the existing page; the other supplies a different value for a fixture.
For authentication shared by tests, Playwright’s authentication guide shows how to define a storageState override in a shared fixture module and have test files import that module’s test object.
Use project dependencies for project-wide prerequisites
A fixture is the natural choice when a resource belongs to an individual test or worker. When a prerequisite belongs to a project workflow—such as setup that must finish before a dependent project runs—use Playwright project dependencies instead. A setup project can run tests before dependent projects and can attach teardown to the setup project; multiple dependency projects can run in parallel. This is a project-level ordering mechanism, not a substitute for per-test fixture lifecycle. See the project dependencies documentation and the global setup and teardown guide for the broader setup options.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Keep larger fixture suites modular and diagnosable
Compose fixture modules
As a suite grows, separate fixture ownership by concern—for example, database helpers in one module and accessibility helpers in another. Playwright’s mergeTests can combine exported fixtures into a test object, so tests can use the helpers together without turning one fixture file into the owner of every concern. The fixtures guide documents fixture composition.
Make report steps useful
Custom fixtures are visible by default as steps in UI mode, Trace Viewer, reports, and error output. A custom title can give a fixture a clearer report label. The box option can hide an uninteresting fixture from visible steps; box: 'self' hides the fixture entry while retaining its nested work. Use boxing for routine plumbing, not for meaningful setup whose visibility would help explain a failure. See Playwright’s fixtures guide for reporting options.
Quick Recap
A practical fixture design checklist
- Give each fixture one clear responsibility and a name that tells tests what resource or behavior they receive.
- Use test scope for state that must be fresh or independently mutable; use worker scope only when reuse is intentional and shared-state behavior is controlled.
- Represent setup dependencies in fixture parameters so Playwright can order setup and teardown correctly.
- Keep resource cleanup after
await use(value)and make it specific to what setup created. - Make optional behavior explicit; reserve automatic fixtures for work that truly belongs in every test or worker.
- When a failure occurs, inspect fixture setup and teardown time separately from the test body, and keep report steps visible when they provide diagnostic value.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




