Playwright + TS · Lesson 16 of 36

0/36 done

Module 16: Browser, Context & Page

16. Browser Hierarchy: Browser vs Context vs Page

17 minlesson

Try it yourself

Implement createIsolatedSessionConfig:

  1. Return ContextOptions object.
  2. Viewport must be width 1280, height 720, and permissions must include ["geolocation"].

The editor has a worked example. Read it, tweak it, then press Mark as complete.

The Hierarchy Explained

text
Browser (1 Heavy OS Process, e.g. Chromium)
  ├── BrowserContext 1 (Isolated incognito profile: cookies, cache, storage)
  │     ├── Page 1 (Tab / Window)
  │     └── Page 2 (Second Tab)
  └── BrowserContext 2 (Totally independent user session, e.g. Admin vs Buyer)
        └── Page 1

Context Isolation in Playwright Test

By default, every single test receives a fresh page fixture. Behind the scenes:

  1. Playwright creates a new BrowserContext in under 20ms.
  2. Creates a Page inside it.
  3. Closes the context when the test finishes.
  4. Zero state leak between tests!

Manual Multi-User Session Testing

typescript
import { test } from "@playwright/test";

test("multi-user chat interaction", async ({ browser }) => {
  // User 1: Alice
  const aliceContext = await browser.newContext();
  const alicePage = await aliceContext.newPage();
  await alicePage.goto("https://chat.targetqa.com");
  await alicePage.fill("#msg", "Hello Bob!");
  await alicePage.click("#send");

  // User 2: Bob (Completely isolated storage and cookies!)
  const bobContext = await browser.newContext();
  const bobPage = await bobContext.newPage();
  await bobContext.grantPermissions(["notifications"]);
  await bobPage.goto("https://chat.targetqa.com");

  // Verify Bob received message
  await bobPage.getByText("Hello Bob!").waitFor();

  await aliceContext.close();
  await bobContext.close();
});

Key Takeaways & Walkthrough Summary

One of Playwright's greatest strengths is its BrowserContext architecture. In traditional tools, starting a new session meant launching an entire heavy browser process. In Playwright, browser contexts act like ultra-fast incognito profiles created in milliseconds. In this lesson, we will explore browsers, contexts, pages, multi-tab handling, and storage isolation. Let's take a look!

Part of the Playwright + TS course (Playwright with TypeScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Why not launch a new Browser for each test?

Launching a browser process takes 1-3 seconds and consumes hundreds of MBs of RAM. Creating a BrowserContext takes 10 milliseconds and shares the underlying browser binary while guaranteeing 100% cookie and storage isolation.

How do I test multi-tab navigation?

Use 'context.waitForEvent("page")' right before clicking a link with target="_blank" to capture the new Page instance.

Finished reading?

Review the worked example in the editor, then mark the lesson complete.

TypeScript
Skip