Back to All Questions
Question 97 of 100
Framework
Intermediate
How does Playwright ensure test isolation?
The Answer
Playwright ensures isolation through two mechanisms: (1) Each test gets a fresh `BrowserContext` (no shared cookies/storage), and (2) Workers are separate OS processes (no shared memory).
Deep Dive Explanation
True isolation means: test A cannot affect test B's browser state. Playwright achieves this via BrowserContexts (cheap isolated sessions) automatically provisioned by the `context` and `page` fixtures. This is why Playwright tests are safe to run in parallel without cross-contamination.
example.spec.ts
// Playwright Test automatically provides isolated fixtures:
test('test A', async ({ page, context }) => {
await context.addCookies([{ name: 'pref', value: 'dark', domain: 'app.com' }]);
await page.goto('/settings');
// Only this test sees the 'dark' cookie
});
test('test B', async ({ page }) => {
const cookies = await page.context().cookies();
// cookies is EMPTY - completely isolated from test A
expect(cookies).toHaveLength(0);
});
// Global setup/teardown for DB seeding (runs once)
// playwright.config.ts
export default defineConfig({
globalSetup: './global-setup.ts',
globalTeardown: './global-teardown.ts',
});