Architecture

Browser Context in Playwright

A browser context is an isolated browser session with its own cookies, storage, permissions, viewport, and authentication state.

Definition and Brief Explanation

A browser context is like a fresh browser profile. Tests can create separate contexts for different users, roles, devices, permissions, or locations without leaking state.

Lifecycle

  1. Create context from browser or test fixture.
  2. Configure storageState, viewport, permissions, locale, or geolocation.
  3. Open one or more pages.
  4. Close the context to remove session state.

Example

Admin context

const context = await browser.newContext({ storageState: 'admin.json' });
const page = await context.newPage();
await page.goto('/admin');

Explanation: The admin session stays isolated from other contexts.

Common Confusion

  • Cookies in one context are not visible in another.
  • Sharing one context across unrelated tests causes leakage.
  • Parallel tests need separate data or isolated users.