Playwright Fixtures and Parameterization
This chapter introduces Playwright's Fixture mechanism, how to create custom Fixtures, and techniques for parameterizing tests.
What Is a Fixture
A Fixture is a dependency injection mechanism for Playwright test function parameters.
Each test obtains the Fixtures it needs through destructured parameters (such as { page }); Playwright handles creation and cleanup.
Example
// page and context are built-in Fixtures
test
(
'Using Built-in Fixtures'
,
async
(
{
page
,
context
}
)
=>
{
// Playwright automatically creates page and context
// Automatically cleaned up after the test ends
await page.
goto
(
'https://www.runoob.com/'
)
;
}
)
;
Built-in Fixture Quick Reference
| Fixture | Type | Description |
|---|---|---|
page |
Page | An independent browser tab |
context |
BrowserContext | Browser context |
browser |
Browser | Browser instance |
browserName |
string | Current browser name |
request |
APIRequestContext | HTTP request client |
Custom Fixtures
Pass test.extend() Creating a custom Fixture lets you inject extra dependencies or override built-in Fixtures.
Example
// File path: tests/fixtures.ts
import
{
test as base
,
expect
}
from
'@playwright/test'
;
// Define the types for custom Fixtures
type MyFixtures
=
{
todoPage
:
{
goto
:
(
)
=>
Promise
<
void
;
addTodo
:
(
text
:
string
)
=>
Promise
<
void
};
authenticatedPage
:
any
;
}
;
export
const
test
=
base.
extend
<
MyFixtures
(
{// Custom Fixture: Todo page object
todoPage
:
async
(
{
page
}
,
use
)
=>
{
// Create
const
todoPage
=
{
goto
:
async
(
)
=>
{
await page.
goto
(
'https://demo.playwright.dev/todomvc'
)
;
}
,
addTodo
:
async
(
text
:
string
)
=>
{
await page.
getByPlaceholder
(
'What needs to be done?'
)
.
fill
(
text
)
;
await page.
keyboard
.
press
(
'Enter'
)
;
}
,
}
;
// Inject into tests
await use
(
todoPage
)
;
// Cleanup logic (if needed)
}
,
// Custom Fixture: authenticated page
authenticatedPage
:
async
(
{
browser
}
,
use
)
=>
{
const
context
=
await browser.
newContext
(
{
storageState
:
'playwright/.auth/user.json'
,
}
)
;
const
page
=
await context.
newPage
(
)
;
await use
(
page
)
;
await context.
close
(
)
;
}
,
}
)
;
export
{
expect
}
;
Using custom Fixtures
Example
// File path: tests/todo.spec.ts
import
{
test
,
expect
}
from
'./fixtures'
;
test
(
'Add Todo'
,
async
(
{
todoPage
}
)
=>
{
await todoPage.
goto
(
)
;
await todoPage.
addTodo
(
'Learn Playwright'
)
;
await todoPage.
addTodo
(
'Master Fixtures'
)
;
// Verify...
}
)
;
Fixture scope
Fixtures have three scopes, controlled via the scope option.
| Scope | Value | Lifetime |
|---|---|---|
| Test scope (default) | scope: 'test' |
Created once per test |
| Worker scope | scope: 'worker' |
Created once per Worker process, shared by all tests within the same Worker |
Example
// Worker-scoped Fixture (shared by all tests within the same Worker)
const
test
=
base.
extend
(
{
sharedResource
:
[
async
(
{
}
,
use
)
=>
{
const
resource
=
await createExpensiveResource
(
)
;
await use
(
resource
)
;
await resource.
dispose
(
)
;
}
,
{
scope
:
'worker'
}
]
,
// Note the array syntax
}
)
;
Worker-scoped Fixtures are shared among multiple tests within the same Worker process, making them suitable for resources that are expensive to create, such as database connections.
Override built-in Fixtures
You can override built-in Fixtures to change the default behavior.
Example
const
test
=
base.
extend
(
{
// Override the built-in page fixture to change the default viewport
page
:
async
(
{
baseURL
,
page
}
,
use
)
=>
{
await page.
setViewportSize
(
{
width
:
1920
,
height
:
1080
}
)
;
await use
(
page
)
;
}
,
// Override storageState
storageState
:
'playwright/.auth/admin.json'
,
}
)
;
Automatic Fixture cleanup
A Fixture's cleanup logic runs after use() is called, similar to the afterEach effect of it.
Example
const
test
=
base.
extend
(
{
tempUser
:
async
(
{
request
}
,
use
)
=>
{
// Create a temporary user
const
resp
=
await request.
post
(
'/api/users'
,
{
data
:
{
name
:
'temp_'
Date
.
now
(
)
}
,
}
)
;
const
user
=
await resp.
json
(
)
;
// Inject into the test
await use
(
user
)
;
// Automatic cleanup: delete the temporary user
await request.
delete
(
`
/
api
/
users
/
$
{
user.
id
}
`
)
;
}
,
}
)
;
Parameterized Tests
Playwright has no built-in parameterization mechanism, but tests can be generated dynamically via loops.
Example
// Data-driven testing
const
testCases
=
[
{
username
:
'admin'
,
password
:
'admin123'
,
expected
:
'Dashboard'
}
,
{
username
:
'user'
,
password
:
'user123'
,
expected
:
'Profile'
}
,
{
username
:
'guest'
,
password
:
'guest123'
,
expected
:
'Guest page'
}
,
]
;
for
(
const
{
username
,
password
,
expected
}
of testCases
)
{
test
(
`User $
{
username
}
sees after login $
{
expected
}
`
,
async
(
{
page
}
)
=>
{
await page.
goto
(
'/login'
)
;
await page.
getByLabel
(
'Username'
)
.
fill
(
username
)
;
await page.
getByLabel
(
'Password'
)
.
fill
(
password
)
;
await page.
getByRole
(
'button'
,
{
name
:
'Login'
}
)
.
click
(
)
;
await expect
(
page.
getByText
(
expected
)
)
.
toBeVisible
(
)
;
}
)
;
}
Test Annotations
Annotations are used to add metadata to tests, which can be viewed and filtered in reporters.
Example
test
(
'Test with annotations'
,
async
(
{
page
}
)
=>
{
// Add annotations (inside the test function body)
test.
info
(
)
.
annotations
.
push
(
{
type
:
'issue'
,
description
:
'https://github.com/runoob/bug/123'
,
}
)
;
test.
info
(
)
.
annotations
.
push
(
{
type
:
'description'
,
description
:
'This test covers all branches of the login flow'
,
}
)
;
// Test logic...
}
)
;
// Use tags (in the test title)
test
(
'Fast smoke test @smoke @critical'
,
async
(
{
page
}
)
=>
{
// Use npx playwright test --grep "@smoke" to run only this test
}
)
;