Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk17 min

Fixtures (Built-in Fixtures) in Playwright

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Playwright’s built-in fixtures give tests immediate access to the resources they need, without requiring manual setup for every scenario. Instead of launching browsers, creating contexts, opening pages, or wiring API clients by hand, tests can receive ready-to-use objects such as page, context, browser, request, and testInfo directly from the test runner.

These fixtures are central to how Playwright keeps tests concise, isolated, and reliable. Their lifecycle and scope determine when resources are created, shared, and disposed, which affects everything from browser session isolation to performance in large test suites.

As an Amazon Associate I earn from qualifying purchases.

Understanding how built-in fixtures work makes it easier to write maintainable end-to-end and API tests, reuse setup patterns safely, and avoid common mistakes such as leaking state between tests or overusing shared browser resources.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What Are Built-in Fixtures in Playwright?

Built-in fixtures in Playwright are ready-to-use objects that the Playwright Test runner creates and passes into your tests automatically. Instead of manually launching a browser, creating a browser context, opening a page, or wiring API clients before every test, you declare the resources you need in the test function parameters. Playwright then prepares them at the right time, injects them into the test, and cleans them up afterward.

A typical Playwright test uses object destructuring to request fixtures by name. For example, page gives you a fresh browser page for interacting with the application UI, while request gives you an API request context for calling backend endpoints. This makes tests shorter and more focused on behavior rather than setup code. The fixture name is significant: when you write a test with { page }, Playwright knows it must create a page before the test starts.

Common built-in fixtures

  • page: A fresh page for browser automation, commonly used for navigation, clicking, filling forms, and assertions against the UI.
  • context: An isolated browser context, similar to a clean browser profile, with separate cookies, local storage, permissions, and session state.
  • browser: The browser instance used to create additional contexts or pages when a test needs more control than the default page fixture provides.
  • request: An API testing client for sending HTTP requests directly from tests, useful for setup, teardown, health checks, and backend assertions.
  • browserName: The current browser engine name, such as chromium, firefox, or webkit, often used for browser-specific behavior.
  • testInfo: Metadata and utilities for the running test, including the test title, retry number, project information, output paths, and attachment support.

Fixtures are one of the main reasons Playwright tests are concise. A test can directly express what it needs, and Playwright handles the surrounding lifecycle. For example, a UI test can use page for browser actions, an API-assisted test can use both page and request, and a diagnostic test can use testInfo to attach screenshots or logs to the report. These resources are available without importing or instantiating them inside each test body.

Built-in fixtures also promote isolation. The page fixture belongs to a fresh context by default, so cookies, storage, and permissions do not leak between tests. This helps prevent hidden dependencies where one test passes only because another test ran before it. At the same time, fixtures have defined scopes: some are created per test, while others are associated with the worker process that runs a group of tests. Understanding that distinction helps you write tests that are both reliable and efficient.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In practice, built-in fixtures act as the foundation for maintainable Playwright test suites. They standardize how browser and API resources are obtained, reduce repeated setup code, and make dependencies visible in the test signature. When a test asks for page, request, or testInfo, a reader can immediately see which capabilities the test relies on, making the suite easier to review, refactor, and scale.

Core Test Fixtures: page, context, and browser

The most commonly used Playwright test fixtures are page, context, and browser. Together, they represent the main layers of browser automation: a browser process, an isolated browser context, and a single tab or page inside that context. In most tests, you will interact primarily with page, while context and browser are useful when you need more control over isolation, permissions, storage, or mulle pages.

page: the default fixture for UI interaction

The page fixture gives each test a ready-to-use browser tab. It is the fixture you use for navigation, clicking, filling forms, making assertions against the DOM, and observing browser behavior. Playwright creates this page automatically before the test runs and closes it after the test finishes, so you do not need to manually create or clean up a tab for ordinary UI tests.

  • Navigate: await page.goto('/login')
  • Interact: await page.getByRole('button', { name: 'Sign in' }).click()
  • Assert: await expect(page.getByText('Dashboard')).toBeVisible()

A typical test uses page directly from the test callback. This keeps tests concise and readable because the setup is handled by the Playwright test runner. The page fixture is test-scoped, which means each test gets its own fresh page. This helps prevent state from leaking between tests and makes failures easier to reproduce.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

context: an isolated browser session

The context fixture represents a browser context, which is similar to a clean, separate browser profile. It has its own cookies, local storage, session storage, permissions, geolocation, viewport settings, and other session-level data. The default page is created inside this context, so any changes to cookies or storage made by the page belong to that test’s isolated session.

You can use context when a test needs to control session-level behavior. Common examples include granting permissions, adding cookies, clearing storage, recording traces or videos through configuration, or creating additional pages in the same signed-in session. For example, a test for a chat application might use const secondPage = await context.newPage() to open another tab as the same user and verify that notifications update across tabs.

  • Multiple tabs: open extra pages with context.newPage().
  • Cookies and storage: inspect or modify session data with context.cookies() and context.addCookies().
  • Permissions: grant capabilities such as clipboard, camera, or geolocation when supported.
  • Network behavior: apply routes that affect all pages in the context.

browser: the shared browser instance

The browser fixture gives access to the underlying browser instance, such as Chromium, Firefox, or WebKit. It is lower level than page and context. In routine tests, you rarely need it because Playwright already creates a context and page for you. It becomes useful when a test must create a completely separate context, such as testing two independent users with different authentication states.

For example, an admin-and-customer workflow can use browser.newContext() to create one isolated context for the admin and another for the customer. Each context can have its own storage state and pages, allowing the test to verify interactions between separate accounts without mixing cookies or sessions. When creating contexts or pages manually from browser, close them when finished to avoid unnecessary resource usage during large test runs.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Fixture Represents Common use
page A single browser tab Most UI actions and assertions
context An isolated browser session Cookies, permissions, storage, multiple tabs
browser The browser instance Creating separate contexts for independent users

API and Worker Fixtures: request, browserName, and testInfo

Beyond browser automation, Playwright Test includes built-in fixtures that help with API calls, project-aware branching, and runtime metadata. The most commonly used ones in this group are request, browserName, and testInfo. They are injected into a test the same way as page or context, by destructuring them from the first argument passed to the test function.

request: API testing without opening a page

The request fixture provides an APIRequestContext, which lets tests make HTTP requests directly from Playwright. This is useful for pure API tests, setup steps, teardown steps, and hybrid UI/API scenarios. For example, a test can create a user through an API endpoint, sign in through the UI, and then verify the newly created user appears in the application. Because API calls are faster and less brittle than repeating every setup step in the browser, request often helps keep end-to-end tests focused on the behavior under test.

  • Create test data: call a backend endpoint before visiting the UI.
  • Verify server state: check an order, profile, or database-backed resource through an API response.
  • Clean up data: delete test-created records after assertions complete.
  • Test APIs directly: write fast checks for status codes, headers, and response bodies without launching a browser page.

browserName: adapting behavior per browser project

The browserName fixture exposes the current browser engine name, such as chromium, firefox, or webkit. It is especially helpful when the same test suite runs across mulle Playwright projects. Most tests should avoid browser-specific branching, since application behavior should usually be consistent across engines. Still, there are valid cases: skipping a test for an unsupported browser feature, applying a different expectation for media behavior, or collecting browser-specific diagnostics.

A maintainable pattern is to use browserName sparingly and keep browser-specific conditions small and explicit. For instance, if a feature relies on a browser capability that is not available in WebKit, the test can skip only that scenario for webkit instead of duplicating the entire test file. This keeps the suite readable while still allowing accurate cross-browser coverage.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

testInfo: runtime details, attachments, and annotations

The testInfo fixture gives access to metadata about the currently running test. It includes details such as the test title, file path, retry count, project name, timeout, output directory, and current status. It also supports attaching files or text to reports, which is valuable when diagnosing failures in CI. Screenshots, logs, API responses, generated files, and custom debug data can be attached through testInfo so they appear with the test result.

Fixture Typical use Scope in practice
request Send HTTP requests for setup, assertions, cleanup, or API-only tests Available per test, with request context managed by Playwright
browserName Read the current browser engine and handle limited browser-specific cases Provided from the current project configuration
testInfo Access test metadata, attach artifacts, inspect retry and project details Unique to the currently executing test

Used together, these fixtures support clean and practical tests: request prepares or verifies backend state, browserName keeps cross-browser behavior controlled, and testInfo enriches reporting when something fails. The best results come from using them for clearly defined responsibilities rather than mixing setup, browser , and diagnostics throughout every test.

Fixture Lifecycle, Scope, and Isolation

Playwright’s built-in fixtures are created and disposed according to their scope. Understanding that lifecycle is essential for predictable tests, especially when a suite grows beyond a few simple browser checks. The most commonly used fixture, page, is test-scoped: each test gets a fresh page instance, and Playwright closes it automatically after the test finishes. The same test-scoped behavior applies to context in typical usage, giving each test an isolated browser context with its own cookies, local storage, permissions, and session state unless you explicitly configure otherwise.

This isolation is one of the main reasons Playwright tests can run reliably in parallel. A login performed in one test does not leak into another test through shared cookies. A route mock, geolocation setting, permission grant, or local storage change belongs to the browser context used by that test. When the test ends, Playwright tears down the resources and starts clean for the next one. This makes built-in fixtures safer than manually sharing pages or contexts across tests.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Test-scoped and worker-scoped fixtures

Fixtures such as page, context, and testInfo are tied to an individual test run. By contrast, some fixtures are effectively worker-scoped or stable for the duration of a worker process. For example, browser is shared by tests running in the same worker, while each test still receives its own isolated context and page. This balances performance and correctness: launching a browser is relatively expensive, while creating a new context is fast and provides strong separation between tests.

Fixture Typical scope Lifecycle behavior
page Test Created for one test and closed after it completes.
context Test Isolated browser session for a single test.
browser Worker Reused by tests in a worker to reduce startup cost.
request Test or configured scope Provides an API request context for HTTP calls and cleanup.
testInfo Test Exposes metadata, attachments, output paths, retry count, and status for the current test.

Fixture setup happens before the test body uses the fixture, and teardown happens after the test completes, including when the test fails. This matters for cleanup. If a test creates data through the request fixture, you can remove that data in an afterEach hook or attach diagnostics with testInfo before teardown finishes. Playwright’s ordering keeps resources available long enough for screenshots, traces, logs, and failure artifacts to be collected.

How scope affects test design

Good test design works with fixture isolation instead of fighting it. For UI tests, prefer starting from a known state inside each test: navigate with page.goto(), seed data through request, and rely on the fresh context to avoid stale sessions. If a test needs an authenticated user, use configured storage state or a setup project rather than logging in repeatedly through the UI. This keeps tests fast while preserving isolation between roles, accounts, and browser sessions.

  • Use page for actions and assertions inside a single browser tab.
  • Use context when a test needs multiple pages, custom permissions, cookies, or storage inspection.
  • Use browser directly only when creating special contexts manually, such as a second isolated session in the same test.
  • Use request for API setup, backend assertions, and test data cleanup.
  • Use testInfo for per-test artifacts, retry-aware behavior, and output file paths.

A common pitfall is storing fixture objects in module-level variables and reusing them across tests. That bypasses Playwright’s lifecycle guarantees and can cause flaky failures when tests run in parallel, retry, or execute in a different order. Another common issue is assuming that data isolation in the browser also isolates backend state. Browser contexts isolate client-side state, but records created in a database, queue, or external service must still be uniquely named or explicitly cleaned up. Treat built-in fixtures as scoped resources, keep shared mutable state out of test files, and let Playwright manage setup and teardown wherever possible.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Using Built-in Fixtures in Real Test Scenarios

Built-in fixtures become most useful when they are combined to model how a user or system actually interacts with an application. In day-to-day Playwright tests, page usually drives browser actions, context controls isolation and browser state, request prepares or verifies backend data, and testInfo helps attach diagnostics when something fails. Keeping each fixture focused makes tests easier to read and avoids turning one test into a long setup script.

Testing a user flow with page and request

A common pattern is to use the request fixture to create test data through an API, then use page to verify the same data in the UI. For example, an e-commerce test might create a product or discount code through an API endpoint, open the storefront with page.goto(), add the product to the cart, and assert that the discount is visible at checkout. This avoids relying on shared seed data and keeps the UI part of the test focused on what the user sees.

  • request: creates users, orders, products, sessions, or feature flags before the UI test starts.
  • page: performs the visible browser interactions, such as filling forms, clicking buttons, and checking text.
  • testInfo: records attachments, annotations, or output paths for screenshots, traces, and custom logs.

Using context for authentication and state

The context fixture is especially useful when a scenario needs controlled browser state. Tests can add cookies, grant permissions, set geolocation, or create isolated sessions without affecting other tests. For authenticated flows, many teams store a signed-in state once during setup and load it through project configuration, but context is still helpful when a test needs a different role, such as an admin, editor, or guest user. Because each test receives a clean context by default, one test changing local storage or cookies will not pollute the next test.

Scenario Useful Fixtures Practical Use
Checkout flow page, request, testInfo Create a cart through the API, complete payment in the UI, attach a screenshot on failure.
Role-based access context, page, browserName Load different storage states and verify permissions across browser projects.
API-backed dashboard request, page Seed reporting data through API calls, then assert charts and table rows in the browser.
Mobile permission flow context, page Grant location or camera permissions and verify the application response.

The browserName fixture is useful when a test needs small browser-specific handling. For instance, a file download, permission prompt, or media codec behavior may differ between Chromium, Firefox, and WebKit. Instead of duplicating the entire test, use browserName to branch only the small part that differs, while keeping the main assertions shared. This keeps cross-browser coverage broad without creating separate test suites for every engine.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In larger suites, built-in fixtures work best when tests are written around user intent rather than fixture mechanics. A maintainable test might use request to create a customer, page to sign in and edit that customer, and request again to verify the backend state after saving. If the test fails, testInfo can provide a stable location for screenshots or JSON responses that help diagnose the failure in CI. This style gives each test reliable setup, isolated execution, and clear evidence when behavior does not match expectations.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Best Practices and Common Pitfalls

Built-in fixtures are most effective when each test uses the smallest resource it needs. For UI tests, prefer the page fixture for normal browser interactions because it already comes from an isolated context and is recreated for every test. Use context only when the test needs context-level behavior, such as granting permissions, adding cookies, setting extra headers, or opening mulle tabs that share the same session. Use browser sparingly, usually when creating custom contexts with special options that cannot be expressed through the default configuration.

A common mistake is to share state through globals instead of relying on fixture isolation. Playwright creates a fresh page and context for each test, so tests should not depend on data left behind by a previous test. If a test signs in, creates records, or changes settings, make that setup explicit in the test, in a project configuration, or in a dedicated setup flow. This keeps tests repeatable whether they run alone, in parallel, or as part of the full suite.

Practical guidelines

  • Use page for most UI flows: actions such as goto, click, fill, and assertions against locators usually belong at the page level.
  • Use request for backend setup: create users, seed records, or clean up test data through the request fixture instead of driving every setup step through the UI.
  • Use testInfo for artifacts and metadata: attach logs, screenshots, generated files, or API responses to the current test report when they help diagnose failures.
  • Keep browserName checks limited: conditional behavior based on browserName is useful for browser-specific coverage, but too many branches can hide inconsistent product behavior.
  • Prefer configuration over inline setup: set base URL, viewport, permissions, storage state, trace mode, and retries in the Playwright config when they apply broadly.

Another frequent pitfall is creating new browser contexts without closing them. The built-in context fixture is cleaned up automatically, but contexts created manually with browser.newContext() should be closed when the test is finished. The same applies to pages opened manually from those contexts. Leaking contexts can increase memory use, slow down the suite, and produce confusing behavior in later tests, especially in large parallel runs.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Be careful with authentication state. Reusing a saved storage state can make tests much faster, but it should represent a stable and intentional starting point. Avoid modifying the same account from many parallel tests, because shared server-side state can still collide even when browser contexts are isolated. A safer pattern is to create unique users or records per test with request, using the test title, worker index, or timestamp as part of the generated data.

Fixture Good use Common pitfall
page Single-tab UI testing with isolated state Expecting it to preserve data across tests
context Permissions, cookies, headers, multiple tabs Using it when page-level actions are enough
browser Creating custom contexts for special cases Forgetting to close manually created contexts
request API setup, teardown, and direct assertions Mixing API state with UI state without clear cleanup
testInfo Attachments, output paths, retry-aware diagnostics Using it for business data instead of test metadata

Maintainable tests treat fixtures as clear boundaries: browser resources come from Playwright, test data is created deliberately, and cleanup is predictable. When built-in fixtures are used consistently, tests become shorter, easier to parallelize, and less dependent on execution order.

Frequently Asked Questions

When should I use page instead of context or browser in a Playwright test?

Use page for most UI tests because it gives you a ready-to-use browser tab with automatic isolation per test. Use context when you need to control browser-level settings such as permissions, geolocation, cookies, or mulle pages in the same session. Use browser only when you need to create custom contexts yourself or test behavior across multiple isolated sessions.

Does Playwright create a new page and context for every test?

Yes, the built-in page fixture is created inside a fresh context for each test, which helps prevent state leakage between tests. This means cookies, local storage, session storage, and permissions do not carry over unless you explicitly configure shared storage state. This default isolation is one of the main reasons Playwright tests are reliable when run in parallel.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How is the request fixture different from making API calls through the browser page?

The request fixture gives you an API testing client that can send HTTP requests directly without using the browser UI. It is useful for setting up test data, cleaning up records, validating backend responses, or testing API endpoints directly. In contrast, requests made through page happen as part of browser interactions and include browser behavior such as navigation, scripts, cookies, and rendering.

Can I use built-in fixtures with beforeEach and afterEach hooks?

Yes, built-in fixtures such as page, context, request, and testInfo can be used in Playwright hooks. For example, you can navigate to a starting URL in beforeEach, attach debugging files with testInfo, or clean up test data with request in afterEach. Keep hook focused and predictable so failures are easy to trace.

What are common mistakes when using Playwright built-in fixtures?

A common mistake is sharing mutable state between tests instead of relying on Playwright’s per-test isolation. Another is using browser for routine tests when page or context would be simpler and safer. Avoid putting too much setup in hooks, and prefer clear fixtures or helper functions for repeated authentication, data setup, and navigation flows.

Bottom Line

Playwright’s built-in fixtures give you dependable, ready-to-use resources like page, context, browser, request, and testInfo without forcing you to manage setup and cleanup manually. Understanding their scope and lifecycle helps you write tests that are isolated, predictable, and easier to debug.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use the simplest fixture that fits the job, keep test state isolated, and rely on configuration or custom fixtures only when repeated setup starts cluttering your tests. As a next step, review your current test suite for duplicated setup code and replace it with the appropriate built-in fixture patterns.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.