2021-01-01 15:17:27 -08:00
|
|
|
---
|
|
|
|
id: multi-pages
|
|
|
|
title: "Multi-page scenarios"
|
|
|
|
---
|
2020-12-30 18:04:51 -08:00
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
Playwright can automate scenarios that span multiple browser contexts or multiple tabs in a browser window.
|
2020-12-30 18:04:51 -08:00
|
|
|
|
2021-01-01 15:17:27 -08:00
|
|
|
<!-- TOC -->
|
2020-12-30 18:04:51 -08:00
|
|
|
|
|
|
|
## Multiple contexts
|
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
[Browser contexts](./core-concepts.md#browser-contexts) are isolated environments on a single browser instance.
|
|
|
|
Playwright can create multiple browser contexts within a single scenario. This is useful when you want to test for
|
2020-12-30 18:04:51 -08:00
|
|
|
multi-user functionality, like chat.
|
|
|
|
|
|
|
|
```js
|
|
|
|
const { chromium } = require('playwright');
|
|
|
|
|
|
|
|
// Create a Chromium browser instance
|
|
|
|
const browser = await chromium.launch();
|
|
|
|
|
|
|
|
// Create two isolated browser contexts
|
|
|
|
const userContext = await browser.newContext();
|
|
|
|
const adminContext = await browser.newContext();
|
|
|
|
|
2021-05-15 10:56:10 -07:00
|
|
|
// Create pages and interact with contexts independently
|
2020-12-30 18:04:51 -08:00
|
|
|
```
|
|
|
|
|
2021-03-01 09:18:44 -08:00
|
|
|
```java
|
|
|
|
import com.microsoft.playwright.*;
|
|
|
|
|
|
|
|
public class Example {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
try (Playwright playwright = Playwright.create()) {
|
|
|
|
BrowserType chromium = playwright.chromium();
|
|
|
|
// Create a Chromium browser instance
|
|
|
|
Browser browser = chromium.launch();
|
|
|
|
// Create two isolated browser contexts
|
|
|
|
BrowserContext userContext = browser.newContext();
|
|
|
|
BrowserContext adminContext = browser.newContext();
|
2021-05-15 10:56:10 -07:00
|
|
|
// Create pages and interact with contexts independently
|
2021-03-01 09:18:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
```python async
|
|
|
|
import asyncio
|
|
|
|
from playwright.async_api import async_playwright
|
|
|
|
|
|
|
|
async def run(playwright):
|
|
|
|
# create a chromium browser instance
|
|
|
|
chromium = playwright.chromium
|
|
|
|
browser = await chromium.launch()
|
|
|
|
|
|
|
|
# create two isolated browser contexts
|
|
|
|
user_context = await browser.new_context()
|
|
|
|
admin_context = await browser.new_context()
|
|
|
|
|
2021-05-15 10:56:10 -07:00
|
|
|
# create pages and interact with contexts independently
|
2021-01-14 15:01:39 -08:00
|
|
|
|
|
|
|
async def main():
|
|
|
|
async with async_playwright() as playwright:
|
|
|
|
await run(playwright)
|
|
|
|
asyncio.run(main())
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
from playwright.sync_api import sync_playwright
|
|
|
|
|
|
|
|
def run(playwright):
|
|
|
|
# create a chromium browser instance
|
|
|
|
chromium = playwright.chromium
|
|
|
|
browser = chromium.launch()
|
|
|
|
|
|
|
|
# create two isolated browser contexts
|
|
|
|
user_context = browser.new_context()
|
|
|
|
admin_context = browser.new_context()
|
|
|
|
|
2021-05-15 10:56:10 -07:00
|
|
|
# create pages and interact with contexts independently
|
2021-01-14 15:01:39 -08:00
|
|
|
|
|
|
|
with sync_playwright() as playwright:
|
|
|
|
run(playwright)
|
|
|
|
```
|
2020-12-30 18:04:51 -08:00
|
|
|
|
2021-05-15 10:56:10 -07:00
|
|
|
```csharp
|
|
|
|
using Microsoft.Playwright;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
2021-05-22 07:55:53 -07:00
|
|
|
class Program
|
2021-05-15 10:56:10 -07:00
|
|
|
{
|
|
|
|
public static async Task Main()
|
|
|
|
{
|
|
|
|
using var playwright = await Playwright.CreateAsync();
|
|
|
|
// Create a Chromium browser instance
|
|
|
|
await using var browser = await playwright.Chromium.LaunchAsync();
|
|
|
|
await using var userContext = await browser.NewContextAsync();
|
|
|
|
await using var adminContext = await browser.NewContextAsync();
|
|
|
|
// Create pages and interact with contexts independently.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-01-17 21:09:40 -08:00
|
|
|
### API reference
|
2020-12-30 18:04:51 -08:00
|
|
|
- [BrowserContext]
|
|
|
|
- [`method: Browser.newContext`]
|
|
|
|
- [`method: BrowserContext.addCookies`]
|
|
|
|
|
|
|
|
## Multiple pages
|
|
|
|
|
|
|
|
Each browser context can host multiple pages (tabs).
|
2021-01-14 15:01:39 -08:00
|
|
|
* Each page behaves like a focused, active page. Bringing the page to front is not required.
|
|
|
|
* Pages inside a context respect context-level emulation, like viewport sizes, custom network routes or browser
|
|
|
|
locale.
|
2020-12-30 18:04:51 -08:00
|
|
|
|
|
|
|
```js
|
|
|
|
// Create two pages
|
|
|
|
const pageOne = await context.newPage();
|
|
|
|
const pageTwo = await context.newPage();
|
|
|
|
|
|
|
|
// Get pages of a brower context
|
|
|
|
const allPages = context.pages();
|
|
|
|
```
|
|
|
|
|
2021-03-01 09:18:44 -08:00
|
|
|
```java
|
|
|
|
// Create two pages
|
|
|
|
Page pageOne = context.newPage();
|
|
|
|
Page pageTwo = context.newPage();
|
|
|
|
|
|
|
|
// Get pages of a brower context
|
|
|
|
List<Page> allPages = context.pages();
|
|
|
|
```
|
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
```python async
|
|
|
|
# create two pages
|
|
|
|
page_one = await context.new_page()
|
|
|
|
page_two = await context.new_page()
|
|
|
|
|
|
|
|
# get pages of a brower context
|
|
|
|
all_pages = context.pages()
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
# create two pages
|
|
|
|
page_one = context.new_page()
|
|
|
|
page_two = context.new_page()
|
2020-12-30 18:04:51 -08:00
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
# get pages of a brower context
|
|
|
|
all_pages = context.pages()
|
|
|
|
```
|
|
|
|
|
2021-05-15 10:56:10 -07:00
|
|
|
```csharp
|
|
|
|
// Create two pages
|
|
|
|
var pageOne = await context.NewPageAsync();
|
|
|
|
var pageTwo = await context.NewPageAsync();
|
|
|
|
|
|
|
|
// Get pages of a brower context
|
|
|
|
var allPages = context.Pages;
|
|
|
|
```
|
|
|
|
|
2021-01-17 21:09:40 -08:00
|
|
|
### API reference
|
2020-12-30 18:04:51 -08:00
|
|
|
- [Page]
|
|
|
|
- [`method: BrowserContext.newPage`]
|
|
|
|
- [`method: BrowserContext.pages`]
|
|
|
|
|
|
|
|
## Handling new pages
|
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
The `page` event on browser contexts can be used to get new pages that are created in the context. This can be used to
|
|
|
|
handle new pages opened by `target="_blank"` links.
|
2020-12-30 18:04:51 -08:00
|
|
|
|
|
|
|
```js
|
|
|
|
// Get page after a specific action (e.g. clicking a link)
|
|
|
|
const [newPage] = await Promise.all([
|
|
|
|
context.waitForEvent('page'),
|
|
|
|
page.click('a[target="_blank"]') // Opens a new tab
|
|
|
|
])
|
|
|
|
await newPage.waitForLoadState();
|
|
|
|
console.log(await newPage.title());
|
|
|
|
```
|
|
|
|
|
2021-03-01 09:18:44 -08:00
|
|
|
```java
|
|
|
|
// Get page after a specific action (e.g. clicking a link)
|
|
|
|
Page newPage = context.waitForPage(() -> {
|
|
|
|
page.click("a[target='_blank']"); // Opens a new tab
|
|
|
|
});
|
|
|
|
newPage.waitForLoadState();
|
|
|
|
System.out.println(newPage.title());
|
|
|
|
```
|
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
```python async
|
|
|
|
# Get page after a specific action (e.g. clicking a link)
|
|
|
|
async with context.expect_page() as new_page_info:
|
|
|
|
await page.click('a[target="_blank"]') # Opens a new tab
|
|
|
|
new_page = await new_page_info.value
|
|
|
|
|
|
|
|
await new_page.wait_for_load_state()
|
|
|
|
print(await new_page.title())
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
# Get page after a specific action (e.g. clicking a link)
|
|
|
|
with context.expect_page() as new_page_info:
|
|
|
|
page.click('a[target="_blank"]') # Opens a new tab
|
|
|
|
new_page = new_page_info.value
|
|
|
|
|
|
|
|
new_page.wait_for_load_state()
|
|
|
|
print(new_page.title())
|
|
|
|
```
|
|
|
|
|
2021-05-15 10:56:10 -07:00
|
|
|
```csharp
|
|
|
|
// Get page after a specific action (e.g. clicking a link)
|
2021-05-19 17:19:25 -07:00
|
|
|
var newPage = await context.RunAndWaitForEventAsync(BrowserContextEvent.Page, async () =>
|
|
|
|
{
|
|
|
|
await page.ClickAsync("a[target='_blank']");
|
|
|
|
});
|
|
|
|
await newPage.WaitForLoadStateAsync();
|
2021-05-15 10:56:10 -07:00
|
|
|
Console.WriteLine(await newPage.TitleAsync());
|
|
|
|
```
|
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
If the action that triggers the new page is unknown, the following pattern can be used.
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Get all new pages (including popups) in the context
|
|
|
|
context.on('page', async page => {
|
|
|
|
await page.waitForLoadState();
|
2021-01-14 15:01:39 -08:00
|
|
|
console.log(await page.title());
|
2020-12-30 18:04:51 -08:00
|
|
|
})
|
|
|
|
```
|
|
|
|
|
2021-03-01 09:18:44 -08:00
|
|
|
```java
|
|
|
|
// Get all new pages (including popups) in the context
|
|
|
|
context.onPage(page -> {
|
|
|
|
page.waitForLoadState();
|
|
|
|
System.out.println(page.title());
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
```python async
|
|
|
|
# Get all new pages (including popups) in the context
|
|
|
|
async def handle_page(page):
|
|
|
|
await page.wait_for_load_state()
|
|
|
|
print(await page.title())
|
|
|
|
|
|
|
|
context.on("page", handle_page)
|
|
|
|
```
|
2020-12-30 18:04:51 -08:00
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
```python sync
|
|
|
|
# Get all new pages (including popups) in the context
|
|
|
|
def handle_page(page):
|
|
|
|
page.wait_for_load_state()
|
|
|
|
print(page.title())
|
|
|
|
|
|
|
|
context.on("page", handle_page)
|
|
|
|
```
|
|
|
|
|
2021-05-15 10:56:10 -07:00
|
|
|
```csharp
|
|
|
|
// Get all new pages (including popups) in the context
|
|
|
|
context.Page += async (_, page) => {
|
|
|
|
await page.WaitForLoadStateAsync();
|
|
|
|
Console.WriteLine(await page.TitleAsync());
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
2021-01-17 21:09:40 -08:00
|
|
|
### API reference
|
2020-12-30 18:04:51 -08:00
|
|
|
- [`event: BrowserContext.page`]
|
|
|
|
|
|
|
|
## Handling popups
|
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
If the page opens a pop-up, you can get a reference to it by listening to the `popup` event on the page.
|
2020-12-30 18:04:51 -08:00
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
This event is emitted in addition to the `browserContext.on('page')` event, but only for popups relevant to this page.
|
2020-12-30 18:04:51 -08:00
|
|
|
|
|
|
|
```js
|
|
|
|
// Get popup after a specific action (e.g., click)
|
|
|
|
const [popup] = await Promise.all([
|
|
|
|
page.waitForEvent('popup'),
|
|
|
|
page.click('#open')
|
|
|
|
]);
|
|
|
|
await popup.waitForLoadState();
|
2021-01-14 15:01:39 -08:00
|
|
|
console.log(await popup.title());
|
|
|
|
```
|
|
|
|
|
2021-03-01 09:18:44 -08:00
|
|
|
```java
|
|
|
|
// Get popup after a specific action (e.g., click)
|
|
|
|
Page popup = page.waitForPopup(() -> {
|
|
|
|
page.click("#open");
|
|
|
|
});
|
|
|
|
popup.waitForLoadState();
|
|
|
|
System.out.println(popup.title());
|
|
|
|
```
|
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
```python async
|
|
|
|
# Get popup after a specific action (e.g., click)
|
|
|
|
async with page.expect_popup() as popup_info:
|
|
|
|
await page.click("#open")
|
|
|
|
popup = await popup_info.value
|
|
|
|
|
|
|
|
await popup.wait_for_load_state()
|
|
|
|
print(await popup.title())
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
# Get popup after a specific action (e.g., click)
|
|
|
|
with page.expect_popup() as popup_info:
|
|
|
|
page.click("#open")
|
|
|
|
popup = popup_info.value
|
|
|
|
|
|
|
|
popup.wait_for_load_state()
|
|
|
|
print(popup.title())
|
2020-12-30 18:04:51 -08:00
|
|
|
```
|
|
|
|
|
2021-05-15 10:56:10 -07:00
|
|
|
```csharp
|
|
|
|
// Get popup after a specific action (e.g., click)
|
2021-05-19 17:19:25 -07:00
|
|
|
var newPage = await page.RunAndWaitForEventAsync(PageEvent.Popup, async () =>
|
|
|
|
{
|
|
|
|
await page.ClickAsync("#open");
|
|
|
|
});
|
2021-05-15 10:56:10 -07:00
|
|
|
await newPage.WaitForLoadStateAsync();
|
|
|
|
Console.WriteLine(await newPage.TitleAsync());
|
|
|
|
```
|
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
If the action that triggers the popup is unknown, the following pattern can be used.
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Get all popups when they open
|
|
|
|
page.on('popup', async popup => {
|
|
|
|
await popup.waitForLoadState();
|
|
|
|
await popup.title();
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
2021-03-01 09:18:44 -08:00
|
|
|
```java
|
|
|
|
// Get all popups when they open
|
|
|
|
page.onPopup(popup -> {
|
|
|
|
popup.waitForLoadState();
|
|
|
|
System.out.println(popup.title());
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
```python async
|
|
|
|
# Get all popups when they open
|
|
|
|
async def handle_popup(popup):
|
|
|
|
await popup.wait_for_load_state()
|
|
|
|
print(await popup.title())
|
2020-12-30 18:04:51 -08:00
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
page.on("popup", handle_popup)
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
# Get all popups when they open
|
|
|
|
def handle_popup(popup):
|
|
|
|
popup.wait_for_load_state()
|
|
|
|
print(popup.title())
|
|
|
|
|
|
|
|
page.on("popup", handle_popup)
|
|
|
|
```
|
|
|
|
|
2021-05-15 10:56:10 -07:00
|
|
|
```csharp
|
|
|
|
// Get all popups when they open
|
|
|
|
page.Popup += async (_, popup) => {
|
|
|
|
await popup.WaitForLoadStateAsync();
|
|
|
|
Console.WriteLine(await page.TitleAsync());
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
2021-01-17 21:09:40 -08:00
|
|
|
### API reference
|
2021-01-14 15:01:39 -08:00
|
|
|
- [`event: Page.popup`]
|