Test Fixtures
Playwright Test is based on the concept of test fixtures. Fixtures are used to establish the environment for each test, giving the test everything it needs and nothing else. Fixtures are automatically set up before the test and torn down after.Importing
Test-Scoped Fixtures
Test-scoped fixtures are created for each test individually. These fixtures are isolated between tests.page
IsolatedPage instance created for each test.
Page
A Playwright Page instance that represents a single tab in the browser. This is the most commonly used fixture.
page.goto(url)- Navigate to a URLpage.getByRole(role, options)- Locate element by ARIA rolepage.getByText(text)- Locate element by text contentpage.getByLabel(label)- Locate element by labelpage.click(selector)- Click an elementpage.fill(selector, value)- Fill an inputpage.screenshot(options)- Take a screenshot
context
IsolatedBrowserContext instance created for each test.
BrowserContext
A browser context that provides isolation between tests. Each context has its own cookies, storage, and cache. The default
page fixture belongs to this context.context.newPage()- Create a new page in this contextcontext.cookies()- Get all cookiescontext.addCookies(cookies)- Add cookiescontext.storageState()- Get storage statecontext.route(pattern, handler)- Route network requestscontext.grantPermissions(permissions)- Grant permissions
request
IsolatedAPIRequestContext instance for making HTTP requests.
APIRequestContext
An API request context for making HTTP requests independent of the browser. Useful for API testing and setup/teardown operations.
request.get(url, options)- Make a GET requestrequest.post(url, options)- Make a POST requestrequest.put(url, options)- Make a PUT requestrequest.delete(url, options)- Make a DELETE requestrequest.fetch(url, options)- Make a custom request
agent
AI agent for autonomous browser interactions.PageAgent
An AI-powered agent that can autonomously interact with the page based on natural language instructions.
Worker-Scoped Fixtures
Worker-scoped fixtures are created once per worker process and shared between all tests in that worker. This makes testing more efficient.browser
SharedBrowser instance for all tests in the worker.
Browser
A browser instance shared between all tests in the same worker. Each test still gets an isolated context and page.
browser.newContext(options)- Create a new browser contextbrowser.newPage(options)- Create a new pagebrowser.contexts()- Get all browser contextsbrowser.version()- Get browser versionbrowser.browserType()- Get browser type (chromium, firefox, webkit)
playwright
Playwright library instance.Playwright
The Playwright library object with methods to launch browsers and access utilities.
playwright.chromium- Chromium browser typeplaywright.firefox- Firefox browser typeplaywright.webkit- WebKit browser typeplaywright.devices- Device descriptorsplaywright.selectors- Selector engineplaywright.request- API request context factory
Custom Fixtures
You can create custom fixtures usingtest.extend().
Example: Page Object Fixture
Example: API Fixture
Example: Worker Fixture
Fixture Options
When defining custom fixtures, you can specify options:TypeScript Definitions
See Also
- Test API - Test functions and methods
- Test Configuration - Configure fixture options
- Custom Fixtures Guide - Learn more about creating fixtures
