Skip to main content

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

Isolated Page 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.
Example:
Key Methods:
  • page.goto(url) - Navigate to a URL
  • page.getByRole(role, options) - Locate element by ARIA role
  • page.getByText(text) - Locate element by text content
  • page.getByLabel(label) - Locate element by label
  • page.click(selector) - Click an element
  • page.fill(selector, value) - Fill an input
  • page.screenshot(options) - Take a screenshot

context

Isolated BrowserContext 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.
Example:
Key Methods:
  • context.newPage() - Create a new page in this context
  • context.cookies() - Get all cookies
  • context.addCookies(cookies) - Add cookies
  • context.storageState() - Get storage state
  • context.route(pattern, handler) - Route network requests
  • context.grantPermissions(permissions) - Grant permissions

request

Isolated APIRequestContext 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.
Example:
Key Methods:
  • request.get(url, options) - Make a GET request
  • request.post(url, options) - Make a POST request
  • request.put(url, options) - Make a PUT request
  • request.delete(url, options) - Make a DELETE request
  • request.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.
Example:
Note: Agent fixtures require configuration with an AI provider. See the Agent Configuration section.

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

Shared Browser 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.
Example:
Key Methods:
  • browser.newContext(options) - Create a new browser context
  • browser.newPage(options) - Create a new page
  • browser.contexts() - Get all browser contexts
  • browser.version() - Get browser version
  • browser.browserType() - Get browser type (chromium, firefox, webkit)

playwright

Playwright library instance.
Playwright
The Playwright library object with methods to launch browsers and access utilities.
Example:
Key Properties:
  • playwright.chromium - Chromium browser type
  • playwright.firefox - Firefox browser type
  • playwright.webkit - WebKit browser type
  • playwright.devices - Device descriptors
  • playwright.selectors - Selector engine
  • playwright.request - API request context factory

Custom Fixtures

You can create custom fixtures using test.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