Playwright + Java · Lesson 16 of 38

0/38 done

Module 16: Playwright Browser Concepts

16. Browser, Context & Page Hierarchy

16 minlesson

Your task

Create a helper method countOpenPages:

  1. Accept BrowserContext context.
  2. Return context.pages().size().

Write it in the editor, then press Check answer (Ctrl + Enter).

The Playwright Hierarchy

  1. Playwright: The top-level connection to the driver.
  2. Browser: The browser process (Chromium, Firefox, WebKit).
  3. BrowserContext: An incognito profile with separate cookies, cache, and localStorage.
  4. Page: A single browser tab or window.
java
try (Playwright playwright = Playwright.create()) {
    Browser browser = playwright.chromium().launch();
    BrowserContext context = browser.newContext();
    Page page = context.newPage();
    page.navigate("https://targetqa.com");
}

Key Takeaways & Walkthrough Summary

Understanding Playwright's hierarchy is crucial. A single Browser instance can host dozens of completely isolated BrowserContexts. Each Context has its own cookies and session storage, enabling multi-user and parallel testing without overhead. Let's master Contexts!

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

Common Doubts & Lesson Q&A

Why use BrowserContext instead of launching multiple Browsers?

BrowserContext creates a full incognito profile in milliseconds with almost zero CPU/RAM overhead, whereas launching a new Browser takes seconds.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Java
Skip