Playwright architecture and test isolation
Playwright drives the browser from outside: the test runs in a separate Node.js process and sends commands over a protocol (CDP for Chromium). Isolation has three levels: Browser (the process, one per worker), BrowserContext (its own cookies, localStorage and cache, created in milliseconds) and Page (a tab). Every test gets a new context, so the previous test's session never reaches it.
Playwright · Architecture
One test logged in, and the next one is already a guest. Where did the session go?
The first test logs in and sees the dashboard. The second one runs right after it in the same worker, opens /cabinet and lands on the sign-in page. The session did not "vanish": Playwright deliberately gave the second test a clean page. This lesson answers two questions: where Playwright lives relative to the browser, and why isolation between tests costs milliseconds instead of seconds.
How it works and why. Playwright is not embedded in the page. The test runs in a separate Node.js process and sends the browser commands (click, navigation, script evaluation), and the browser returns a result or an event. For Chromium this is the Chrome DevTools Protocol (CDP); for Firefox and WebKit Playwright ships its own patched builds with a similar protocol. So the downloaded browser is not an exact copy of a regular Firefox or Safari but a build tied to a specific library version.
Control from outside gives what in-browser tools lack: several tabs, windows and origins in one test, iframes, shadow DOM, file downloads. And browsers already have the idea of a "profile with no shared state" (like an incognito window). Playwright made it the basic unit of isolation:
Browser: a running browser process. Launching takes seconds, so there is one per worker.BrowserContext: an isolated session with its own cookies, localStorage, cache, permissions, viewport, locale and time zone. Creating one takes milliseconds.Page: a single tab inside a context.
What is nested in what
In the runner this layout works without your involvement: browser is one per worker, while context and page are new for every test. That is why the first test's session never reached the second: the first context was destroyed along with its cookies. To avoid logging in through the UI in every test, a saved session (storageState) is plugged into the context in advance.
Example: two users in one test. An admin must see a message a user has just sent. The typical attempt: one page, two logins in turn.
// before: one page, one session
test('admin sees the message', async ({ page }) => {
await login(page, 'user');
await page.getByRole('textbox').fill('Hello');
await page.getByRole('button', { name: 'Send' }).click();
await login(page, 'admin'); // replaces the user's cookies
await expect(page.getByText('Hello')).toBeVisible();
});
The test may pass, but it checks the wrong thing: the admin opened the chat after a fresh login, not at the same time as the user, so live behaviour (real-time updates, notifications) is invisible to it. Two contexts model two people side by side:
// after: two independent contexts
test('admin sees the message', async ({ browser }) => {
const user = await browser.newContext({ storageState: 'auth/user.json' });
const admin = await browser.newContext({ storageState: 'auth/admin.json' });
const userPage = await user.newPage();
const adminPage = await admin.newPage();
await userPage.goto('/chat');
await adminPage.goto('/chat');
await userPage.getByRole('textbox').fill('Hello');
await userPage.getByRole('button', { name: 'Send' }).click();
await expect(adminPage.getByText('Hello')).toBeVisible();
await user.close();
await admin.close();
});
Why it is written this way: contexts created by hand through browser.newContext() are not closed by the runner until the worker ends, so you close them explicitly (otherwise, for example, the video of that context is not saved). The auth/*.json files are produced by a separate setup step, covered in another lesson.
One tab or two contexts
One page, re-login
Cookies of the second login replace the first. The user and the admin are never "in the system" at the same time, so live updates are not verified.
Two contexts
Two independent sessions side by side, like two people on different devices. The "one sends, the other sees" scenario is really verified.
Limits and comparison. Context isolation covers browser state, not the world around it:
- Shared data on the server (the DB, one test account) is not isolated by a context. Two parallel tests with one account can corrupt each other's data.
- Contexts of one browser share a process: if the browser crashes, all its contexts crash. Browser extensions need
launchPersistentContext, not a regular context. - A context does not change the engine: Chromium, Firefox and WebKit are different browsers, run as separate projects.
The difference from Cypress follows from where the code runs:
| Aspect | Playwright | Cypress |
|---|---|---|
| Where the test runs | A Node.js process outside the browser | Test code runs inside the browser next to the app |
| Several tabs and origins | Supported natively, including multiple contexts | Tabs are not controlled natively (only through the @cypress/puppeteer plugin), another origin needs cy.origin() |
| Test isolation | A new BrowserContext in milliseconds | State is cleared between tests (testIsolation) |
Common mistakes.
- Sharing state between tests through module variables or leftovers in the DB: the test passes only in a certain order or only in one worker, because the next test gets a clean context.
- Launching a new
browserin every test: seconds of wasted time, when only a context is needed. - Treating a tab as a separate session: pages of one context share cookies and storage. Two users need two contexts.
- Not closing hand-made contexts: memory grows, and the video of such a context is not saved.
- Assuming Playwright WebKit equals Safari on a device: it is a close engine, not a substitute for a real check.
Check yourself
Test A puts an item in the cart; test B in the same worker opens the cart and finds it empty. Why is this correct behaviour and not a bug?
Answer
Every test gets a new BrowserContext, so cookies and localStorage from test A were destroyed with its context. This is isolation by design: test B must not depend on order. Prepare the cart you need yourself, for example through the API or a fixture.
A test calls context.newPage() twice. Will the second tab see the first one's session?
Answer
Yes: both pages belong to one context and share cookies and storage. If you need an independent session, create a second context with browser.newContext().
Why does Playwright start one browser per worker but a new context per test?
Answer
Launching a browser process takes seconds, a context takes milliseconds. A context gives the same clean state, so the process can be reused and isolation is almost free.