To understand how Playwright works internally, we first need to understand three key components:
1. Browser
2. Browser Context
3. Page
These three objects form the foundation of all UI automation in Playwright. Understanding these concepts helps you design efficient and scalable test executions.
Browser
A Browser represents an actual browser engine instance such as:
- Chromium (Google Chrome / Microsoft Edge)
- Firefox
- WebKit (Safari)
When Playwright launches a browser, it can run in one of the following modes:
1. Headless mode: Runs without a UI, executes faster, and is commonly used in CI/CD pipelines
2. Headed mode: Runs with a visible UI, useful for debugging and test development
Playwright Code to Launch a Browser
Launching a full browser instance is resource-heavy. To avoid opening multiple browsers, Playwright encourages the use of browser contexts.
Browser Context
A Browser Context is one of Playwright’s biggest advantages over other automation tools. It represents an isolated browser session inside a single browser instance, similar to Incognito Mode.
Key Features of Browser Context
- Each context has isolated cookies, cache, and local storage
- In Playwright, each test runs in its own browser context, so tests do not interfere with each other
- Allows simulating multiple users within a single browser instance
- Creating a browser context is faster than launching a new browser
Playwright Code to Create a Browser Context:
Two Browser Contexts in One Browser
This approach is extremely useful for multi-user scenarios.
Each context has its own isolated session, so there are no login or data conflicts.
Page
A Page is similar to a browser tab.
Creating a Page
The web application actually loads inside the page. All user interactions such as clicking, typing, filling forms, dragging, and navigation are performed using the page object.
Open a Website Using Page
Putting It All Together
The following script launches a browser, creates a browser context, opens a page, and navigates to a website:
In real Playwright tests, we usually don’t create the browser, context, and page manually. Playwright fixtures automatically provide a ready-to-use page, handling browser launch, context creation, and cleanup for us.
We will learn about Playwright fixtures in detail in an upcoming tutorial, including how they simplify test code and improve test maintainability.