Playwright BrowserContext: What It Is, Why It Matters, and How to Configure It

Published: (February 6, 2026 at 01:02 PM EST)
6 min read
Source: Dev.to

Source: Dev.to

If you’ve been using Playwright for a while, you’ve definitely used BrowserContext—even if you didn’t fully realize it. It’s one of those core concepts that quietly shapes:

  • test isolation
  • speed & flakiness
  • auth state
  • parallelism
  • overall sanity of your test suite

This article is a practical, real‑world deep dive into:

  • What a BrowserContext actually is (and why it exists)
  • How Playwright creates and manages contexts for you
  • How to configure contexts globally in playwright.config.ts
  • How to share setup across tests without sharing state
  • When and how to use multiple setup files / projects
  • Common anti‑patterns and real‑world use cases

Audience: Engineers who already know Playwright basics and want to level up their test architecture.


What Is a BrowserContext?

A BrowserContext is an isolated browser profile.

One Browser  → the actual Chrome / Firefox / WebKit instance
Multiple BrowserContexts → separate incognito‑like sessions

Each context has its own:

FeatureDescription
CookiesPrivate to the context
LocalStorage / SessionStorageIsolated per context
IndexedDBSeparate storage
CacheNot shared
PermissionsContext‑specific
Auth stateIndependent

All contexts share the same browser process, which makes them fast and cheap.
If you’ve ever opened two incognito windows side‑by‑side, you’ve essentially used two browser contexts.

Playwright’s Default Isolation

test('example', async ({ page }) => {
  // This page lives in a fresh browser context
});

Under the hood

  1. Playwright launches a browser.
  2. Creates a new browser context.
  3. Creates a page inside that context.
  4. Destroys the context after the test.

Benefits

  • No state leakage between tests
  • Safe parallel execution
  • Predictable failures

If you’ve ever fought flaky Selenium tests caused by leftover cookies, this is why Playwright feels so much better.

Note: You rarely need to create a context manually, but it’s useful to know how:

import { chromium } from '@playwright/test';

const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();

Playwright’s test runner does this for every test unless you tell it otherwise.

Key takeaway: Pages never exist without a BrowserContext.

Global Configuration – playwright.config.ts

Most context configuration happens via the use block.

// playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
  use: {
    baseURL: 'https://my-app.com',
    headless: true,
    viewport: { width: 1280, height: 720 },
    locale: 'en-US',
    timezoneId: 'America/New_York',
  },
});

Everything inside use becomes the default BrowserContext options.
Thus every test gets the same viewport, locale, timezone… but still not the same state.

The Killer Feature: storageState

Logging in before every test is:

  • Slow
  • Brittle
  • Redundant

Global setup that creates a reusable auth snapshot

// global-setup.ts
import { chromium } from '@playwright/test';

export default async () => {
  const browser = await chromium.launch();
  const context = await browser.newContext();
  const page = await context.newPage();

  await page.goto('https://my-app.com/login');
  await page.fill('#email', 'user@test.com');
  await page.fill('#password', 'password');
  await page.click('button[type=submit]');

  // Save authenticated state to a file
  await context.storageState({ path: 'auth.json' });
  await browser.close();
};
// playwright.config.ts (continued)
export default defineConfig({
  globalSetup: require.resolve('./global-setup.ts'),
  use: {
    storageState: 'auth.json',
  },
});

Now every test:

  • Starts logged in
  • Still runs in a fresh browser context

This is isolation with convenience.

Common Anti‑Pattern: Sharing Live Pages

let sharedPage;

beforeAll(async ({ browser }) => {
  const context = await browser.newContext();
  sharedPage = await context.newPage();
});

Why it’s bad

  • Breaks test isolation
  • Breaks parallelism
  • Causes order‑dependent failures

Preferred approach – fixtures

// fixtures.ts
import { test as base } from '@playwright/test';

export const test = base.extend({
  authenticatedPage: async ({ page }, use) => {
    await page.goto('/dashboard');
    await use(page);
  },
});

Each test still gets:

  • Its own context
  • Its own page

But the setup logic is shared cleanly.

Multiple Users / Roles

Sometimes you need more than one authenticated user (e.g., chat apps, admin flows).

test('admin invites user', async ({ browser }) => {
  const adminContext = await browser.newContext({ storageState: 'admin.json' });
  const userContext = await browser.newContext({ storageState: 'user.json' });

  const adminPage = await adminContext.newPage();
  const userPage = await userContext.newPage();

  await adminPage.goto('/admin');
  await userPage.goto('/dashboard');
});

Using Projects for Role‑Based Testing

// playwright.config.ts (continued)
export default defineConfig({
  projects: [
    {
      name: 'guest',
      use: { storageState: undefined },
    },
    {
      name: 'user',
      use: { storageState: 'auth.json' },
    },
    {
      name: 'admin',
      use: { storageState: 'admin.json' },
    },
  ],
});
  • Each project runs the same tests.
  • Spins up different browser contexts.
  • Can run in parallel.

Run a specific project:

npx playwright test --project=admin

What Should / Shouldn’t Be Shared

✅ Should be shared❌ Should never be shared
Config (use)Live pages
Storage snapshots (storageState)Live contexts
Fixtures & helpersMutable global state

Remember: One test = one browser context (unless you explicitly create more). This rule explains why Playwright scales, why parallel runs work, and why tests don’t leak state.

Quick Checklist for Flaky Tests

❌ Bad pattern✅ Fix
beforeAll creates a pageLet Playwright create a fresh context per test
Globals holding page/contextUse fixtures or storageState instead
Tests depend on previous navigationKeep each test self‑contained
Repeated UI loginsUse a dedicated auth snapshot (storageState)

TL;DR

  • BrowserContext = isolated profile (cookies, storage, auth, etc.)
  • Playwright creates a fresh context for every test by default.
  • Configure defaults in playwright.config.tsuse.
  • Use global setup + storageState to avoid repetitive logins.
  • Never share live pages/contexts; share only immutable data (config, snapshots, fixtures).
  • Leverage projects for role‑based or device‑based testing.

Happy testing! 🚀

Managing Browser Contexts in Playwright

beforeAll – convenient but risky

// Example (do **not** use this pattern for shared state)
beforeAll(async () => {
  // …
});

Using beforeAll feels convenient, but it breaks:

  • Parallel execution – tests run at the same time can interfere with each other.
  • Isolation guarantees – shared state leaks between tests.
  • Debuggability – failures become harder to trace to a specific test.

Fix: Prefer per‑test setup with fixtures. If something truly must run once, ensure it does not create shared browser state.

Safe ways to share configuration

  • Playwright config options (use, projects, fixtures) are safe to share.
  • Browser contexts, pages, and mutable globals are not safe to share across tests.

If a failure only appears when tests run together, the shared browser state is usually the culprit.

Multi‑user flows: a common pitfall

Some teams try to force complex multi‑user scenarios into a single page. This leads to:

  • Unreadable tests
  • Fake mocks instead of real behavior
  • Missed bugs

Fix: Model each user with its own browser context (or separate pages) intentionally.

Why BrowserContext matters

BrowserContext is one of Playwright’s most important architectural concepts.
When you understand how contexts work, your test suite naturally becomes:

  • Faster – contexts can be created in parallel and reused efficiently.
  • More reliable – each test runs in an isolated environment.
  • Easier to scale – adding new scenarios doesn’t require tangled state.
  • Easier to reason about – the flow of each user is explicit.

Bottom line

If your Playwright tests feel fragile or hard to maintain, the root cause is often how browser contexts are managed.

  • Design around isolated contexts.
  • Use fixtures for per‑test setup.
  • Reserve beforeAll only for truly global, immutable configuration.

When you get the context handling right, the rest of Playwright feels effortless.

Happy testing!

Back to Blog

Related posts

Read more »

Nim

Configuration File nim ~/.config/nim/config.nims import std/strutils, strformat switch'nimcache', fmt'{getCurrentDir}/nimcache/{projectName}/{CompileTime.toHex...