Multi-page scenarios
Playwright can automate scenarios that span multiple browser contexts or multiple tabs in a browser window.
#
Multiple contextsBrowser 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 multi-user functionality, like chat.
- Sync
- Async
#
API reference#
Multiple pagesEach browser context can host multiple pages (tabs).
- 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.
- Sync
- Async
#
API reference#
Handling new pagesThe 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.
- Sync
- Async
If the action that triggers the new page is unknown, the following pattern can be used.
- Sync
- Async
#
API reference#
Handling popupsIf the page opens a pop-up, you can get a reference to it by listening to the popup
event on the page.
This event is emitted in addition to the browserContext.on('page')
event, but only for popups relevant to this page.
- Sync
- Async
If the action that triggers the popup is unknown, the following pattern can be used.
- Sync
- Async