Playwright Context Fixture Not Isolating Fix
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
storageStateto persist authentication across tests - Create separate contexts for parallel scenarios
- Use
context.clearCookies()andcontext.clearPermissions()for cleanup - Configure context defaults in
<a href="/testing-qa/playwright/">playwright</a>.config.ts
Common Mistakes with fixture context
- Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro