Can you explain browser isolation in Playwright?
Here is a classic flaky-suite story. Test A adds three items to the cart. Test B checks that a new visitor sees an empty cart. Run B alone and it passes; run the suite and it fails, because B inherited A's cart. Browser isolation is Playwright's answer to this: each test runs in its own clean browser session, so the order of tests stops mattering.
How Playwright isolates tests
Playwright Test starts one browser per worker process and reuses it, because launching a browser is slow. For each test, it creates a new BrowserContext inside that browser, hands it to the test as the context and page fixtures, and closes it when the test ends. A context is like a brand-new browser profile, so each test starts with:
- no cookies, no
localStorageand nosessionStorage; - an empty cache and no registered service workers;
- no granted permissions (location, notifications, clipboard);
- only the device settings from your config (viewport, locale, time zone).
Seeing it work
// tests/cart.spec.ts
import { test, expect } from '@playwright/test';
test('adds three items to the cart', async ({ page }) => {
await page.goto('/shop');
await page.evaluate(() => localStorage.setItem('cart', '3'));
});
test('a new visitor starts with an empty cart', async ({ page }) => {
await page.goto('/shop');
const cart = await page.evaluate(() => localStorage.getItem('cart'));
expect(cart).toBeNull(); // passes, whatever ran before
});
Running these two tests in one worker, one after the other, the second test still reads null from storage and an empty document.cookie. The first test's data was thrown away with its context.
What is not isolated
Isolation covers the browser session. Several things sit outside it, and they cause most "it only fails in the full run" bugs:
| Shared thing | Why it leaks | What to do |
|---|---|---|
| Module-level variables in the spec file | Tests in the same worker run in the same Node process. In a quick check, a counter set in test A was still 1 in test B on the same worker, but 0 when B ran on a second worker. | Keep state inside the test or in fixtures |
| Your backend and database | All tests hit the same server | Create unique data per test, for example an email with testInfo.workerIndex in it |
| One shared test account | The server may allow only one session per user | Use one account per worker |
beforeAll state | It runs once per worker, not per test | See beforeAll and afterAll hooks |
Sharing a login without breaking isolation
Logging in for every test is slow, but sharing one live context between tests brings the leaks back. The middle path is to log in once, save the session to a file with storageState, and let each test start a fresh context from that file:
// playwright.config.ts (relevant part)
projects: [
{ name: 'setup', testMatch: /auth\.setup\.ts/ },
{
name: 'chromium',
dependencies: ['setup'],
use: { storageState: 'playwright/.auth/user.json' },
},
],
Each test begins logged in, yet anything it changes in the browser still disappears afterwards. The setup file is shown in how Playwright manages cookies.
When you really need shared state
Some flows, like a long checkout split into steps, are easier as one sequence. You can opt in with test.describe.configure({ mode: 'serial' }) and create one page in beforeAll. Use this rarely: if one step fails, the rest are skipped, and the tests can no longer run in parallel.
Interview angle
Compared with tools where tests often reuse one long-lived browser session, Playwright makes a fresh session cheap enough to create for every test. That is what allows safe parallel execution, and it is why a good answer mentions contexts, not just "Playwright opens a new browser".
Want to master Playwright hands-on?
Join Uncodemy's Playwright Automation Testing Course in Noida — Dedicated Placement Support.
Explore the Course →