#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Playwright Interview Q&A · #19

Can you explain browser isolation in Playwright?

By Uncodemy Editorial Team · Updated 16 Nov 2026

Here is a classic flaky-suite story. Test A adds three items to the cart. Test B checks that a new visitor sees an empty cart. Run B alone and it passes; run the suite and it fails, because B inherited A's cart. Browser isolation is Playwright's answer to this: each test runs in its own clean browser session, so the order of tests stops mattering.

How Playwright isolates tests

Playwright Test starts one browser per worker process and reuses it, because launching a browser is slow. For each test, it creates a new BrowserContext inside that browser, hands it to the test as the context and page fixtures, and closes it when the test ends. A context is like a brand-new browser profile, so each test starts with:

Seeing it work

// tests/cart.spec.ts
import { test, expect } from '@playwright/test';

test('adds three items to the cart', async ({ page }) => {
  await page.goto('/shop');
  await page.evaluate(() => localStorage.setItem('cart', '3'));
});

test('a new visitor starts with an empty cart', async ({ page }) => {
  await page.goto('/shop');
  const cart = await page.evaluate(() => localStorage.getItem('cart'));
  expect(cart).toBeNull();          // passes, whatever ran before
});

Running these two tests in one worker, one after the other, the second test still reads null from storage and an empty document.cookie. The first test's data was thrown away with its context.

What is not isolated

Isolation covers the browser session. Several things sit outside it, and they cause most "it only fails in the full run" bugs:

Shared thingWhy it leaksWhat to do
Module-level variables in the spec fileTests in the same worker run in the same Node process. In a quick check, a counter set in test A was still 1 in test B on the same worker, but 0 when B ran on a second worker.Keep state inside the test or in fixtures
Your backend and databaseAll tests hit the same serverCreate unique data per test, for example an email with testInfo.workerIndex in it
One shared test accountThe server may allow only one session per userUse one account per worker
beforeAll stateIt runs once per worker, not per testSee beforeAll and afterAll hooks

Sharing a login without breaking isolation

Logging in for every test is slow, but sharing one live context between tests brings the leaks back. The middle path is to log in once, save the session to a file with storageState, and let each test start a fresh context from that file:

// playwright.config.ts (relevant part)
projects: [
  { name: 'setup', testMatch: /auth\.setup\.ts/ },
  {
    name: 'chromium',
    dependencies: ['setup'],
    use: { storageState: 'playwright/.auth/user.json' },
  },
],

Each test begins logged in, yet anything it changes in the browser still disappears afterwards. The setup file is shown in how Playwright manages cookies.

When you really need shared state

Some flows, like a long checkout split into steps, are easier as one sequence. You can opt in with test.describe.configure({ mode: 'serial' }) and create one page in beforeAll. Use this rarely: if one step fails, the rest are skipped, and the tests can no longer run in parallel.

Interview angle

Compared with tools where tests often reuse one long-lived browser session, Playwright makes a fresh session cheap enough to create for every test. That is what allows safe parallel execution, and it is why a good answer mentions contexts, not just "Playwright opens a new browser".

Want to master Playwright hands-on?

Join Uncodemy's Playwright Automation Testing Course in Noida — Dedicated Placement Support.

Explore the Course →