How do browser contexts in Playwright enable isolated multi-user scenarios within a single test worker?
Asked by The SDET Playbook
Asked Sep 28, 2026Viewed 0 times
How do browser contexts in Playwright enable isolated multi-user scenarios within a single test worker?
Asked by The SDET Playbook
Sign in to answer and to vote.
A BrowserContext is an isolated browser session inside one browser process, with its own cookies, local storage and cache. Creating one is fast, so a single test can play several users at once, for example two people in the same chat:
import { test, expect } from '@playwright/test';
test('a message reaches the other user', async ({ browser }) => {
const aliceContext = await browser.newContext({ storageState: 'playwright/.auth/alice.json' });
const bobContext = await browser.newContext({ storageState: 'playwright/.auth/bob.json' });
try {
const alice = await aliceContext.newPage();
const bob = await bobContext.newPage();
await alice.goto('/chat');
await bob.goto('/chat');
await alice.getByRole('textbox', { name: 'Message' }).fill('Hi Bob');
await alice.getByRole('button', { name: 'Send' }).click();
await expect(bob.getByText('Hi Bob')).toBeVisible();
} finally {
await aliceContext.close();
await bobContext.close();
}
});
The relative goto('/chat') works because contexts created with browser.newContext() inside Playwright Test pick up baseURL from the config. Each context starts from its own saved sign-in state, and closing the contexts at the end frees their resources. The page fixture already gives each test its own context, so you only create extra ones when a test needs more than one user.
Sources: Playwright browser contexts, Playwright use options, Playwright authentication