Built-in fixtures and manual creation
The runner creates and closes the built-in fixtures itself, while a context created manually is closed by the test author. The browser is launched once per worker, and a fresh context and page are created for every test. A browser.newContext() context is used when another profile is needed and is closed in a fixture after await use(), so it closes even after a failure and videos and HARs are saved.
Playwright · Browser, Context, Page
A test asks only for page. So where does the context live, and who closes the one I created myself?
Almost every test starts with async ({ page }), which makes it easy to miss that three different objects with different lifetimes stand behind it. While one page is enough, this does not matter. Trouble starts when one test needs another locale, a second user, or a page without a saved login, and a failed test leaves no video in the report. This lesson explains who creates and closes what, and how to build your own contexts so that nothing leaks.
How it works and why. Browser, BrowserContext and Page are nested in each other. Browser is a running browser process, an expensive thing. BrowserContext is an isolated "profile" inside that browser, similar to an incognito window: its own cookies, localStorage, locale and time zone settings. Creating a context is cheap, many times faster than launching a browser. Page is a tab in a context.
Hence the runner's decision: the browser is launched once per worker and shared between tests (worker scope), while each test gets a fresh context and page (test scope). Tests are isolated at the session level without paying for a browser launch every time. One more detail: after a failed test the worker is shut down, and the next test runs in a new worker with a new browser. The runner does this to guarantee a clean state after a failure.
Who contains whom and how long it lives
| Fixture | Scope | Created | Closed by |
|---|---|---|---|
browser | worker | Once per worker | Runner, after the worker |
context | test | For every test | Runner, after the test |
page | test | For every test, in context | Runner, with the context |
The page fixture depends on context, and context on browser. So a test that asks only for page still has a context: you can get it by adding context to the parameter list, and it is the same object.
Context options. Most of what you write in the config's use is a context option (the rest controls the browser launch and the runner: headless, trace, testIdAttribute): baseURL, viewport, locale, timezoneId, storageState, permissions, extraHTTPHeaders, colorScheme, geolocation. For a file or a group you change them with test.use():
import { test, expect } from '@playwright/test';
test.describe('localization', () => {
test.use({ locale: 'uk-UA', timezoneId: 'Europe/Kyiv', colorScheme: 'dark' });
test('the date is shown in Kyiv time', async ({ page }) => {
await page.goto('/orders');
await expect(page.getByTestId('created-at')).toContainText('грудня');
});
});
browser.newContext() accepts the same options. A context created by hand in a test also inherits the context options from use, and values passed explicitly to newContext override them.
Example: a custom context that leaks. One test needs another locale. The typical first version looks tidy but hides a problem:
// before: the context is closed only if the test reaches the end
test('guest sees the cookie banner', async ({ browser }) => {
const context = await browser.newContext({ locale: 'uk-UA' });
const page = await context.newPage();
await page.goto('/');
await expect(page.getByRole('dialog', { name: 'Cookies' })).toBeVisible();
await context.close();
});
What is wrong here. If expect fails, it throws, and the context.close() line does not run. The runner does not treat a manually created context as its own, so closing it is the test author's job. Tabs stay open until the end of the worker. And if the context was given recordVideo or recordHar explicitly, the files are written only when it closes, so they may be missing for exactly the failed test. The config video option does not apply to manual contexts.
// after: closing in a fixture, teardown runs even after a failure
import { test as base, type BrowserContext } from '@playwright/test';
export const test = base.extend<{ ukContext: BrowserContext }>({
ukContext: async ({ browser }, use) => {
const context = await browser.newContext({ locale: 'uk-UA' });
await use(context); // the test runs here
await context.close(); // runs even if the test failed
},
});
Why this is right: the runner always executes the code after await use(), even when the test failed. A plain try/finally inside the test works too, but it must be repeated in every test. A fixture is written once, and the test just asks for ukContext and knows nothing about closing.
Built-in context or your own newContext
Built-in context and test.use()
One user and one configuration for a test or a group. The runner creates and closes it, nothing leaks. Choose this by default.
browser.newContext() in a test
Needed only when one test has several profiles: another locale, another storageState, a second user. Closing becomes your debt, so hide it in a fixture.
Limits and alternatives. Context options apply to the whole context: locale, timezoneId and storageState do not change after creation, while, for example, the window size can be changed with page.setViewportSize(). If part of the tests needs another configuration, it is easier to move it to a separate project in the config than to create contexts by hand. In Selenium isolation usually means a separate WebDriver session or clearing cookies; in Cypress since version 12 testIsolation clears the page and cookies between tests by default. In Playwright isolation is built into the model, and a cheap context is its foundation.
Common mistakes.
- Creating a context by hand without guaranteed closing: after a failure tabs stay open and recorded video or HAR are not finalised.
- Calling
chromium.launch()in a test instead of thebrowserfixture: every test pays for a browser launch, and the runner knows nothing about that browser. - Trying to change
localeorstorageStateof an existing context: the options are set at creation. - Copying the same options into every
newContext()instead of one place: the config,test.use()or a fixture. - Keeping state between tests in module variables because "the context is the same": it is fresh for each test, and only the browser is shared.
Check yourself
A test creates a context with browser.newContext(), and an expect in the middle fails. What happens to the context.close() line at the end, and why does it hurt?
Answer
The exception from expect interrupts the function, so close() never runs. Tabs stay open until the end of the worker, and the video and HAR of that context are finalised on close, so they may be missing for exactly the failed test. The fix: a fixture with close() after await use(), or try/finally.
Why do two tests in the same worker not see each other's cookies even though they use one browser?
Answer
Cookies and storage belong to the context, not the browser. Each test gets a new context, and the browser is only a container in which contexts are isolated from each other. The browser is shared for speed, and isolation comes from contexts.
browser.newContext() only when a test needs a different profile, and I close it in a fixture after await use(), so teardown runs even on failure and the video and HAR are kept. I would not launch a browser inside a test: it is expensive and goes around worker management.