Browser Context

Web Automationadvanced

// Definition

An isolated browser session with its own cookies, storage, and cache — like a fresh incognito window, but programmatic. Playwright's `browser.newContext()` creates one; each is fully independent. Contexts let one test run exercise multiple users simultaneously (e.g. a chat between user A and user B) without cross-contamination.

// Why it matters

Browser contexts are how QA tests multi-user and isolation scenarios cleanly — two logged-in users in one test, or guaranteeing no leftover session bleeds between tests. They're faster than launching whole browsers and are the modern answer to test independence at the session level. Cypress handles this differently (per-test clearing); Playwright makes contexts explicit.

// How to test

// Playwright: two isolated users in one test (e.g. real-time chat)
const userA = await browser.newContext()
const userB = await browser.newContext()
const pageA = await userA.newPage()
const pageB = await userB.newPage()
await pageA.goto('/chat'); await pageB.goto('/chat')
// A sends, B receives — fully isolated sessions, no cookie bleed

// Common mistakes

  • Reusing one context across tests so session state leaks between them
  • Spinning up whole browsers when a new context would be faster and lighter
  • Assuming Cypress and Playwright isolate sessions identically (they differ)

// Related terms