Playwright Multiple Tabs and Windows Explained: A Practical Guide
Modern web applications do not always keep everything inside one browser tab. A button may open a report in another tab, a payment process may launch a separate window, or an authentication flow may create a popup.
This creates an important question for automation testers:
How do you handle multiple tabs and windows in Playwright?
The good news is that Playwright makes this simpler than it may first appear. Playwright treats a browser tab or popup window as a Page. A BrowserContext can contain multiple pages, so your test can work with several tabs without manually switching between them like a human user would.
For testers building a strong foundation, the Playwright automation learning resources can also be useful alongside hands-on practice.
What Does “Multiple Tabs” Mean in Playwright?
In Playwright, a browser tab is represented by a Page object.
For example:
const page = await context.newPage();
await page.goto('https://example.com');
If another tab opens in the same browser context, Playwright creates another Page.
Think about it like this:
This distinction is important because a tab is not a browser context.
A context is an isolated browser environment. It can contain one or many pages.
Browser vs Browser Context vs Page
A beginner can understand the relationship like this:
Browser
|
+-- Browser Context
|
+-- Page 1
+-- Page 2
+-- Page 3
The browser is the overall browser process.
The browser context is an isolated testing environment.
Each page represents a tab or popup inside that context.
For example:
const browser = await chromium.launch();
const context = await browser.newContext();
const page1 = await context.newPage();
const page2 = await context.newPage();
await page1.goto('https://example.com');
await page2.goto('https://playwright.dev');
Now page1 and page2 are two independent pages inside the same context.
This model becomes especially useful when you need to test workflows involving several pages.
If you are still learning Playwright fundamentals, it is useful to understand Playwright installation and the basic test structure before moving into advanced page handling.
How to Open Multiple Pages
You can explicitly create multiple pages:
const pageOne = await context.newPage();
const pageTwo = await context.newPage();
await pageOne.goto('https://example.com');
await pageTwo.goto('https://playwright.dev');
console.log(await pageOne.title());
console.log(await pageTwo.title());
There is no special “switch tab” command here.
You simply keep references to the pages you want to use.
That is one of the biggest differences between Playwright automation and manual browser interaction.
How to Detect a New Tab
Suppose a page contains a link that opens another tab.
Do not click first and start looking for the new page afterward.
Instead, start waiting for the event before clicking.
const newPagePromise = context.waitForEvent('page');
await page.getByRole('link', { name: 'Open Report' }).click();
const newPage = await newPagePromise;
await newPage.waitForLoadState();
console.log(await newPage.title());
What the code does
context.waitForEvent('page') tells Playwright:
“A new page is expected. Give me that page when it appears.”
The click then triggers the new tab.
Finally, newPage contains the newly created page.
Why it works
The new page belongs to the browser context, so the context can observe its page event. Playwright specifically recommends waiting for the event before performing the action that causes the new page to appear.
When to use it
Use this approach when a link, button, JavaScript action, or application workflow opens a new page.
Handling a Popup
There is a related API:
const popupPromise = page.waitForEvent('popup');
await page.getByRole('button', { name: 'Open Window' }).click();
const popup = await popupPromise;
await popup.waitForLoadState();
console.log(await popup.title());
A popup is also a Page, but page.waitForEvent('popup') specifically waits for a popup associated with the current page.
Playwright documents that the popup event is emitted in addition to the browser-context page event, but the popup event is scoped to the page that opened it.
Page event vs popup event
Example: Working With Two Tabs
Imagine an application where the first tab contains customer information and clicking “View Invoice” opens the invoice in another tab.
const invoicePromise = context.waitForEvent('page');
await page.getByRole('link', { name: 'View Invoice' }).click();
const invoicePage = await invoicePromise;
await invoicePage.waitForLoadState();
await invoicePage.getByRole('heading', { name: 'Invoice' }).waitFor();
console.log(await invoicePage.title());
The original page remains available.
You do not need to close it or manually switch away from it.
You can interact with both:
await page.getByText('Customer Details').isVisible();
await invoicePage.getByText('Invoice Total').isVisible();
This makes multi-page workflows much easier to express.
Getting All Open Pages
Sometimes you do not know exactly which page will be created.
Playwright provides:
const pages = context.pages();
console.log(`Open pages: ${pages.length}`);
This returns the pages currently belonging to the browser context.
For example:
for (const currentPage of context.pages()) {
console.log(await currentPage.title());
}
This can be useful for debugging or applications where several pages may be opened dynamically.
However, do not use context.pages() as your primary synchronization strategy when you already know which action creates a new page.
Waiting for the expected event is usually clearer and less fragile.
Closing an Additional Tab
When you are finished with a page:
await newPage.close();
You can also close a popup:
await popup.close();
This is useful when a test intentionally opens temporary pages.
For example:
const popupPromise = page.waitForEvent('popup');
await page.getByText('Open Report').click();
const popup = await popupPromise;
await popup.waitForLoadState();
console.log(await popup.title());
await popup.close();
The original page remains available.
Multiple Browser Contexts Are Different
Now consider a different requirement.
You want to test an application as:
Admin
Normal user
Using two pages inside one context may not provide the isolation you need.
Instead, create separate contexts:
const adminContext = await browser.newContext();
const userContext = await browser.newContext();
const adminPage = await adminContext.newPage();
const userPage = await userContext.newPage();
Now each context can have its own cookies, storage, permissions, and other browser state.
This is useful for multi-user testing.
For authentication-heavy applications, this concept connects naturally with Playwright authentication testing and reusable test setup.
Real-World Uses of Multiple Tabs
Multiple-page automation is useful in many applications.
1. Reports
A dashboard opens a PDF or report in a new tab.
Your test can capture the new page and verify its URL or content.
2. Payment workflows
A payment provider may open another window.
Your test can wait for the popup and continue the transaction flow.
3. Authentication
Some systems open a separate authentication window.
The test can capture that page and interact with it.
4. Documentation links
An application may open external documentation in another tab.
The test can verify that the correct destination was opened.
5. Customer portals
An application may open invoices, statements, or downloadable documents in separate pages.
Common Mistakes
Mistake 1: Waiting after clicking
This is fragile:
await page.getByText('Open').click();
const popup = await page.waitForEvent('popup');
The event may already have happened.
Prefer:
const popupPromise = page.waitForEvent('popup');
await page.getByText('Open').click();
const popup = await popupPromise;
Mistake 2: Confusing pages with contexts
Three tabs do not mean three browser contexts.
You can have:
1 Browser
1 Context
3 Pages
or:
1 Browser
2 Contexts
Context A → 2 Pages
Context B → 1 Page
Mistake 3: Using arbitrary timeouts
Avoid:
await page.waitForTimeout(3000);
just because a popup might open.
Wait for the actual event instead.
Mistake 4: Using page indexes blindly
Code such as:
const pages = context.pages();
const secondPage = pages[1];
can work in controlled situations, but it becomes fragile when other pages are opened.
Event-based synchronization is usually easier to maintain.
Debugging Multiple-Page Tests
When a multi-tab test fails, print useful information:
for (const currentPage of context.pages()) {
console.log('URL:', currentPage.url());
console.log('Title:', await currentPage.title());
}
Also check:
Did the click actually happen?
Was the new page created?
Was the expected event used?
Did the popup navigate?
Did the test accidentally close the new page?
Is the page inside the expected context?
Playwright's trace and debugging capabilities can also help you understand what happened during the test.
For broader Playwright learning, resources such as a Playwright roadmap and Playwright projects can help connect individual techniques to larger automation frameworks.
Best Practices
For reliable multiple-tab automation:
Treat every tab as a Page.
Treat browser contexts as isolation boundaries.
Wait for page or popup events before the triggering action.
Keep page references in meaningful variables.
Avoid arbitrary sleep statements.
Close temporary pages when appropriate.
Use separate contexts for independent users.
Use locators instead of brittle selectors.
Use tracing when a multi-page test behaves unexpectedly.
If you are improving your locator skills, the Playwright Locators guide is a natural next step because reliable locators are still important when interacting with the newly opened page.
Frequently Asked Questions
Does Playwright support multiple tabs?
Yes. A browser context can contain multiple Page objects, and each page can represent a tab or popup window.
How do I wait for a new tab in Playwright?
Use context.waitForEvent('page') before the action that opens the new page.
How do I handle a popup?
Use page.waitForEvent('popup') before clicking the element that creates the popup.
Can Playwright control two tabs at the same time?
Yes. Keep references to both Page objects and interact with them independently.
Is a Playwright page the same as a browser context?
No. A page represents a tab or popup. A browser context is an isolated browser session that can contain multiple pages.
How do I close a second tab?
Call await secondPage.close().
Conclusion
Handling multiple tabs in Playwright becomes much easier once you understand its object model.
A browser contains contexts. A context can contain multiple pages. A page represents a tab or popup.
For expected new tabs, use context.waitForEvent('page'). For popups created by a particular page, use page.waitForEvent('popup'). Keep the resulting page reference and interact with it normally.
The important lesson is that Playwright does not require you to think like a human manually switching browser tabs. Instead, you work directly with the Page objects representing those tabs.
That approach leads to clearer, more reliable multi-page automation.
Official reference: Playwright Pages documentation.

Comments
Post a Comment