65 / 139 · 13 Browser Automation with Playwright · Contexts, Waiting, and Assertions← prev⊞ allnext →☰ Read as one page
6.2Browser Contexts
A browser context is a lightweight, isolated browser session — like an incognito window. Each context has its own cookies, localStorage, and cache. Unlike Selenium, where each test typically launches a new browser process, Playwright creates contexts within a single browser instance.
// Each test gets its own context automatically in @playwright/test
test('user A sees their dashboard', async ({ page }) => {
// `page` belongs to a fresh context — isolated from other tests
});
// Manual context creation for multi-user scenarios
test('two users collaborate', async ({ browser }) => {
const adminContext = await browser.newContext();
const userContext = await browser.newContext();
const adminPage = await adminContext.newPage();
const userPage = await userContext.newPage();
// Admin and user have completely separate sessions
await adminPage.goto('/admin');
await userPage.goto('/dashboard');
});
Why Contexts Matter
| Selenium Approach | Playwright Approach |
|---|---|
| One browser per test (slow startup) | One browser, many contexts (fast) |
| Shared state leaks between tests | Complete isolation per context |
| Parallel = multiple browser processes | Parallel = multiple contexts in one browser |
| Cookie cleanup between tests is manual | Contexts are disposable — no cleanup needed |