Skip to main content

Introduction

Fixtures are Playwright’s dependency injection system for tests. They provide a way to set up the test environment and share objects between tests.

Built-in Fixtures

Playwright provides several built-in fixtures from src/index.ts:71-520:

Browser Fixtures

Browser
Shared browser instance across tests in the same worker
string
Browser name: ‘chromium’, ‘firefox’, or ‘webkit’From src/index.ts:73:
BrowserContext
Isolated browser context for each test
Page
Isolated page for each test

Request Fixtures

APIRequestContext
API request context for testing REST APIsFrom src/index.ts:505-519:

Fixture Scopes

From src/common/fixtures.ts:25-27, fixtures have two scopes:

Test-Scoped Fixtures

Created for each test:

Worker-Scoped Fixtures

Shared across all tests in a worker:

Creating Custom Fixtures

Extend the base test with custom fixtures:

Using Custom Fixtures

Fixture Options

From src/common/fixtures.ts:28-29,108-119:

Option Fixtures

Fixtures that can be configured:

Auto Fixtures

Fixtures that run automatically: From src/common/fixtures.ts:26,38-39:

Fixture Timeout

From src/common/fixtures.ts:44-45:

Fixture Dependencies

Fixtures can depend on other fixtures: From src/common/fixtures.ts:46-47,231-237:

Fixture Dependency Rules

From src/common/fixtures.ts:164-230:
  1. Scope Compatibility: Test-scoped fixtures can depend on worker-scoped fixtures, but not vice versa
  2. No Circular Dependencies: Fixtures cannot have circular dependencies
  3. Override Inheritance: Fixtures can override previous definitions using super

Advanced Fixture Patterns

Page Object Model

Authenticated Context

Shared Test Data

Fixture Debugging

From src/common/fixtures.ts:249-251:

Built-in Fixture Configuration

From src/index.ts:71-157:

Fixture Execution Order

Fixtures are initialized in dependency order:

Merging Test Types

From src/common/testType.ts:315-326:
mergeTests combines fixtures from multiple test types while avoiding duplication of shared fixtures.

Best Practices

  1. Keep fixtures focused: Each fixture should have a single responsibility
  2. Use appropriate scope: Worker-scoped fixtures for expensive operations
  3. Avoid side effects: Fixtures should be independent and not affect each other
  4. Clean up resources: Always clean up in the teardown phase
  5. Type your fixtures: Use TypeScript for better IntelliSense

Next Steps

Test Hooks

Learn about beforeEach and afterEach hooks

Configuration

Configure fixture options