-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: add new doc for multi-page scenarios #2737
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5dc434e
docs: multi-page scenarios
arjunattam cdeb6c0
Merge branch 'master' into multi-page
arjunattam 10a7178
docs: multi-page scenarios
arjunattam 74de806
Merge branch 'master' into multi-page
arjunattam de182ec
docs: multi-page scenarios
arjunattam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# Multi-page scenarios | ||
|
||
Playwright can automate scenarios that span multiple browser contexts or multiple | ||
tabs in a browser window. | ||
|
||
<!-- GEN:toc-top-level --> | ||
- [Multiple contexts](#multiple-contexts) | ||
- [Multiple pages](#multiple-pages) | ||
- [Handling new pages](#handling-new-pages) | ||
- [Handling popups](#handling-popups) | ||
<!-- GEN:stop --> | ||
|
||
## Multiple contexts | ||
|
||
[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 | ||
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(); | ||
|
||
// Load user and admin cookies | ||
await userContext.addCookies(userCookies); | ||
await adminContext.addCookies(adminCookies); | ||
``` | ||
|
||
#### API reference | ||
|
||
- [class `BrowserContext`](./api.md#class-browsercontext) | ||
- [`browser.newContext([options])`](./api.md#browsernewcontextoptions) | ||
- [`browserContext.addCookies(cookies)`](api.md#browsercontextaddcookiescookies) | ||
|
||
## Multiple pages | ||
|
||
Each 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. | ||
|
||
```js | ||
// Create two pages | ||
const pageOne = await context.newPage(); | ||
const pageTwo = await context.newPage(); | ||
|
||
// Get pages of a brower context | ||
const allPages = context.pages(); | ||
``` | ||
|
||
#### API reference | ||
|
||
- [class `Page`](./api.md#class-page) | ||
- [`browserContext.newPage()`](./api.md#browsercontextnewpage) | ||
- [`browserContext.pages()`](./api.md#browsercontextpages) | ||
|
||
## Handling new pages | ||
|
||
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. | ||
|
||
```js | ||
// Get page after a specific action (e.g. clicking a link) | ||
const [newPage] = await Promise.all([ | ||
context.waitForEvent('page'), | ||
page.evaluate(() => window.open('https://google.com', '_blank')) | ||
]) | ||
await newPage.waitForLoadState(); | ||
console.log(await newPage.title()); | ||
|
||
// Get all new pages (including popups) in the context | ||
context.on('page', async page => { | ||
await page.waitForLoadState(); | ||
await page.title(); | ||
}) | ||
``` | ||
|
||
#### API reference | ||
|
||
- [event: 'page'](./api.md#event-page) | ||
|
||
## Handling popups | ||
|
||
If 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. | ||
|
||
```js | ||
// Get popup after a specific action (e.g., click) | ||
const [popup] = await Promise.all([ | ||
page.waitForEvent('popup'), | ||
page.click('#open') | ||
]); | ||
await popup.waitForLoadState(); | ||
await popup.title(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
|
||
// Get all popups when they open | ||
page.on('popup', async popup => { | ||
await popup.waitForLoadState(); | ||
await popup.title(); | ||
}) | ||
``` | ||
|
||
#### API reference | ||
|
||
- [event: 'popup'](./api.md#event-popup) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll need at least domcontentloaded before accessing title, but preferably load for everything else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Updated to use
newPage.waitForLoadState()
since that covers more scenarios for the audience of a guide like this.