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 fromsrc/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
Fromsrc/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
Fromsrc/common/fixtures.ts:28-29,108-119:
Option Fixtures
Fixtures that can be configured:Auto Fixtures
Fixtures that run automatically: Fromsrc/common/fixtures.ts:26,38-39:
Fixture Timeout
Fromsrc/common/fixtures.ts:44-45:
Fixture Dependencies
Fixtures can depend on other fixtures: Fromsrc/common/fixtures.ts:46-47,231-237:
Fixture Dependency Rules
Fromsrc/common/fixtures.ts:164-230:
- Scope Compatibility: Test-scoped fixtures can depend on worker-scoped fixtures, but not vice versa
- No Circular Dependencies: Fixtures cannot have circular dependencies
- Override Inheritance: Fixtures can override previous definitions using
super
Advanced Fixture Patterns
Page Object Model
Authenticated Context
Shared Test Data
Fixture Debugging
Fromsrc/common/fixtures.ts:249-251:
Built-in Fixture Configuration
Fromsrc/index.ts:71-157:
Fixture Execution Order
Fixtures are initialized in dependency order:Merging Test Types
Fromsrc/common/testType.ts:315-326:
mergeTests combines fixtures from multiple test types while avoiding duplication of shared fixtures.Best Practices
- Keep fixtures focused: Each fixture should have a single responsibility
- Use appropriate scope: Worker-scoped fixtures for expensive operations
- Avoid side effects: Fixtures should be independent and not affect each other
- Clean up resources: Always clean up in the teardown phase
- Type your fixtures: Use TypeScript for better IntelliSense
Next Steps
Test Hooks
Learn about beforeEach and afterEach hooks
Configuration
Configure fixture options
