Skip to contentPlayForge

What is test isolation in Playwright?

Written by Jatin Thacker, Automation Architect · Updated

Test isolation means each test runs in its own fresh browser context, with its own cookies, local storage and session storage, so nothing one test does can leak into the next. Every test can run alone, in any order, and give the same result.

This answer is one piece of Chapter 30: Flakiness at Scale.

  • Flaky, or Simply Broken
  • Reproducing Flake on Purpose
  • Reading a Trace for the Real Cause
  • Shared State and Test Pollution
  • A Suite the Team Has Stopped Trusting

Playwright's docs say it directly: "each test has its own local storage, session storage, cookies etc." The runner creates a new browser context for every test and gives it a default page. That is why you never need to log out or clear data at the end of a test.

This matters most when a test fails. If test B only passes because test A left something behind, B fails when run alone, when A is skipped, or when the two run in parallel. Playwright's best practices put it as: "Each test should be completely isolated from another test and should run independently."

The practice app stores its cart in local storage, which makes isolation easy to see. In the example, the first test adds the grinder and checks the cart badge reads 1. The second test opens the same page and finds the badge at 0 and the empty-cart message showing. Both ran in order on one worker, and the second still started clean, because it got a new context.

If tests need the same setup, such as being signed in, share it with a beforeEach hook or a fixture, not by relying on an earlier test.

isolation.spec.js(https://practice.learnplayforge.com)
import { test, expect } from '@playwright/test'; test('first test puts the grinder in the cart', async ({ page }) => { await page.goto('/catalog.html?mock=static&seed=42&autologin=1'); await page.locator('article[data-sku="MRD-1003"]') .getByRole('button', { name: 'Add to cart' }).click(); await expect(page.getByTestId('cart-count')).toHaveText('1'); }); test('second test starts with an empty cart', async ({ page }) => { await page.goto('/catalog.html?mock=static&seed=42&autologin=1'); await expect(page.getByTestId('cart-count')).toHaveText('0'); await expect(page.getByTestId('cart-empty')).toBeVisible(); });
Output
$ npx playwright test isolation.spec.js --workers=1
  2 passed (2.8s)
Test yourself
Test A adds an item to a cart stored in local storage. Test B runs straight after it on the same worker. What does B see?

Practised in the full course: the drill Serial mode page sharing.

See the chapter

AI can write the test now. The job is knowing whether it checks anything. That's what the course teaches, one chapter at a time.

163 lessons · 226 drills · start free

← All questions

Training a team? Talk to us.