Skip to content

Playwright Context Fixture Not Isolating Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Playwright Context Fixture Not Isolating Fix. We cover key concepts, practical examples, and best practices.

Tests leak state to each other — localStorage, cookies, or session data from one test affects another. The default browser context isn't properly isolated.

The Problem

// WRONG — sharing browser instance, not context
test.describe('shopping cart', () => {
  test('add item to cart', async ({ page }) => {
    await page.goto('https://example.com');
    await page.evaluate(() => localStorage.setItem('cart', 'item1'));
  });

  test('check cart', async ({ page }) => {
    await page.goto('https://example.com');
    const cart = await page.evaluate(() => localStorage.getItem('cart'));
    // 'cart' is 'item1' from previous test — state leak!
  });
});

The first test sets localStorage data that persists into the second test because they share the same browser context.

Step-by-Step Fix

1. Understand context isolation

// RIGHT — each test gets a new context
import { test, expect } from '@playwright/test';

test('add item to cart', async ({ page, context }) => {
  await page.goto('https://example.com');
  await page.evaluate(() => localStorage.setItem('cart', 'item1'));
  // This localStorage is isolated to this context
});

test('check empty cart', async ({ page }) => {
  await page.goto('https://example.com');
  const cart = await page.evaluate(() => localStorage.getItem('cart'));
  expect(cart).toBeNull(); // Different context — clean state
});

2. Create custom contexts

// RIGHT — manually control context creation
test('authenticated vs anonymous', async ({ browser }) => {
  const authContext = await browser.newContext({
    storageState: './auth.json',
  });
  const anonContext = await browser.newContext();

  const authPage = await authContext.newPage();
  const anonPage = await anonContext.newPage();

  await authPage.goto('https://example.com/profile');
  await anonPage.goto('https://example.com/profile');

  // authPage shows profile, anonPage shows login

  await authContext.close();
  await anonContext.close();
});

3. Share storage across contexts

// RIGHT — export and import storage state
test('save auth state', async ({ page }) => {
  await page.goto('https://example.com/login');
  await page.fill('#email', 'user@example.com');
  await page.fill('#password', 'password');
  await page.click('button[type="submit"]');
  await page.context().storageState({ path: './auth.json' });
});

test('use saved auth', async ({ browser }) => {
  const context = await browser.newContext({
    storageState: './auth.json',
  });
  const page = await context.newPage();
  await page.goto('https://example.com/profile');
  // Already authenticated
});

4. Configure context globally

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

export default defineConfig({
  use: {
    storageState: './auth.json',
    locale: 'en-US',
    timezoneId: 'America/New_York',
    viewport: { width: 1280, height: 720 },
  },
});

Expected output:

✓ add item to cart (0.8s)
✓ check empty cart (0.7s)
✓ save auth state (1.5s)

Prevention Tips

  • Each test gets an isolated browser context by default
  • Use storageState to persist authentication across tests
  • Create separate contexts for parallel scenarios
  • Use context.clearCookies() and context.clearPermissions() for cleanup
  • Configure context defaults in <a href="/testing-qa/playwright/">playwright</a>.config.ts

Common Mistakes with fixture context

  1. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  2. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  3. Using head and tail instead of pattern matching, causing runtime errors on empty lists

These mistakes appear frequently in real-world PLAYWRIGHT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What's the difference between browser and context?

A browser is the overall browser process. A context is an isolated browsing session within that browser — like an incognito window. Each context has its own cookies, localStorage, and cache.

How do I clear context state between tests?

By default, Playwright creates a new context for each test. No manual cleanup needed. If you reuse contexts, use context.clearCookies() and context.storageState({ path: null }) to reset.

Can I share authentication across multiple test files?

Save storage state to a JSON file after login, then configure storageState in <a href="/testing-qa/playwright/">playwright</a>.config.ts to load it for all tests. This avoids re-authenticating in every test file.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro