Fixtures
Relevant source files
The following files were used as context for generating this wiki page:
Fixtures are a central feature of Playwright Test, providing a powerful way to define and manage the environment for tests. They use dependency injection to provide tests with everything they need, from simple values to complex objects. Fixtures handle setup and teardown, ensuring that tests are isolated and that resources are managed efficiently.
The fixture system is designed to be extensible. Users can define their own fixtures or override existing ones using test.extend(). This allows for creating reusable setup logic, sharing objects between tests, and customizing the test environment to fit specific needs. Fixtures can be scoped to a single test or shared across all tests in a worker process.
Architecture
The fixture system is orchestrated by the FixtureRunner, which manages the lifecycle of fixtures. It resolves dependencies, sets up fixtures in the correct order, runs the test, and then tears down the fixtures in reverse order.
Key Components
| Component | Description | Source File |
|---|---|---|
FixtureRunner | Orchestrates the entire lifecycle of fixtures for a test run. It resolves, sets up, and tears down fixtures. | packages/playwright/src/worker/fixtureRunner.ts |
Fixture | An internal class representing a live instance of a fixture, holding its value and managing its state. | packages/playwright/src/worker/fixtureRunner.ts |
FixturePool | A collection of all registered fixtures (FixtureRegistration) for a given test file. It is responsible for resolving fixture names. | packages/playwright/src/common/fixtures.ts |
FixtureRegistration | The definition of a fixture, including its name, scope, implementation function, and dependencies. | packages/playwright/src/common/fixtures.ts |
testInfo | A special fixture that provides information about the currently running test. It is available in both tests and other fixtures. | tests/playwright-test/test-info.spec.ts |
Class Diagram
This diagram shows the relationship between the main components of the fixture system.
Fixture Lifecycle
The FixtureRunner manages the setup and teardown of fixtures. The process ensures that all dependencies for a test are met before it runs and that all resources are cleaned up afterward.
Setup and Teardown Flow
- Dependency Collection: The
FixtureRunnerfirst identifies all fixtures required by a test function, including automatic fixtures. - Order Resolution: It builds a dependency graph and determines the correct setup order.
- Setup Execution: Fixtures are set up one by one. Each fixture's function is executed, and its value is stored. The
usefunction passes the value to the test and waits for the test to complete before proceeding to the teardown phase. - Test Execution: Once all fixtures are set up, the test function is executed with the fixture values injected as parameters.
- Teardown Execution: After the test completes, fixtures are torn down in the reverse order of their setup. This ensures that dependencies are still available for a fixture's teardown logic.
Sequence Diagram
The following diagram illustrates the sequence of operations when running a test with a single fixture dependency.
Fixture Scopes
Fixtures can have two scopes: test and worker. The scope determines the lifecycle and sharing of the fixture's value.
| Scope | Description |
|---|---|
test | The default scope. A new instance of the fixture is created for each test. Setup runs before the test, and teardown runs after. |
worker | A single instance of the fixture is created per worker process. It is shared across all tests running in that worker. Setup runs before the first test in the worker that uses it, and teardown runs after the last test in the worker has finished. |
A worker scoped fixture cannot depend on a test scoped fixture.
Sources: packages/playwright/src/worker/fixtureRunner.ts:311-316, tests/playwright-test/fixtures.spec.ts:200-216
Example of Scopes
// tests/playwright-test/fixtures.spec.ts:200-216
let counter = 0;
import { test as base, expect } from '@playwright/test';
const test = base.extend({
asdf: [ async ({}, test) => await test(counter++), { scope: 'worker' } ],
});
test('should use asdf 1', async ({asdf}) => {
expect(asdf).toBe(0);
});
test('should use asdf 2', async ({asdf}) => {
expect(asdf).toBe(0);
});
test('should use asdf 3', async ({asdf}) => {
expect(asdf).toBe(0);
});
In this example, the asdf fixture is worker-scoped, so it is initialized only once for the worker, and all three tests receive the same value (0). If it were test-scoped, the counter would increment for each test.
Sources: tests/playwright-test/fixtures.spec.ts:200-216
Automatic Fixtures
Fixtures can be configured to run automatically for every test, without being explicitly declared in the test function's parameters. This is useful for setting up logging, tracing, or other background services.
The auto property in a fixture registration can be set to:
true: The fixture runs for every test.'all-hooks-included': The fixture runs for every test and also forbeforeAll/afterAllhooks.false(default): The fixture only runs when explicitly requested by a test or another fixture.
// tests/playwright-test/fixtures.spec.ts:271-287
test.use({
auto: [ async ({}, runTest) => {
console.log('\\n%%auto-setup');
await runTest();
console.log('\\n%%auto-teardown');
}, { auto: true } ],
foo: async ({}, runTest) => {
console.log('\\n%%foo-setup');
await runTest();
console.log('\\n%%foo-teardown');
},
});
test('test 1', async ({ foo }) => {
});
In the example above, the auto fixture will run automatically. The foo fixture is also set up because it's requested by the test. The output demonstrates that automatic fixtures are set up before regular fixtures and torn down after.
Sources: packages/playwright/src/common/fixtures.ts:21, tests/playwright-test/fixtures.spec.ts:271-287
Fixtures in Hooks
Fixtures can be used in hooks (beforeAll, beforeEach, afterEach, afterAll) just like in tests. The FixtureRunner resolves and provides the necessary fixtures for each hook.
beforeEach/afterEachhooks can use bothtestandworkerscoped fixtures.beforeAll/afterAllhooks can only useworkerscoped fixtures, unless the fixture is marked withauto: 'all-hooks-included'.
This diagram illustrates how fixtures are resolved and executed for hooks and tests.
The testInfo Fixture
Playwright provides a built-in testInfo fixture that contains metadata about the current test run. It is a test-scoped fixture and can be accessed in tests, hooks, and other fixtures. It is a crucial mechanism for fixtures to interact with the test's state, such as accessing the output path, title, status, or adding attachments and annotations.
A key aspect is that test.info() can be called to access the testInfo object for the currently running test, but only from within a test or a hook.
// tests/playwright-test/test-info.spec.ts:43-56
test('should work via test.info', async ({ runInlineTest }) => {
const result = await runInlineTest({
'helper.ts': `
import { test as base } from '@playwright/test';
export * from '@playwright/test';
export const test = base.extend({
title: async ({}, run) => {
await run(base.info().title);
},
});
`,
'a.test.js': `
const { test, expect } = require('./helper');
test('test 1', async ({title}) => {
expect(test.info().title).toBe('test 1');
expect(title).toBe('test 1');
});
`,
});
expect(result.exitCode).toBe(0);
});
Sources: tests/playwright-test/test-info.spec.ts, tests/playwright-test/access-data.spec.ts