mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
feat(browsercontext): add BrowserContext.pages() method (#3003)
This commit is contained in:
parent
25d7eff374
commit
c018ff1555
@ -50,6 +50,7 @@ Next Release: **Aug 9, 2018**
|
||||
* [browserContext.close()](#browsercontextclose)
|
||||
* [browserContext.isIncognito()](#browsercontextisincognito)
|
||||
* [browserContext.newPage()](#browsercontextnewpage)
|
||||
* [browserContext.pages()](#browsercontextpages)
|
||||
* [browserContext.targets()](#browsercontexttargets)
|
||||
- [class: Page](#class-page)
|
||||
* [event: 'close'](#event-close)
|
||||
@ -560,6 +561,9 @@ Promise which resolves to a new [Page] object. The [Page] is created in a defaul
|
||||
#### browser.pages()
|
||||
- returns: <[Promise]<[Array]<[Page]>>> Promise which resolves to an array of all open pages. Non visible pages, such as `"background_page"`, will not be listed here. You can find them using [target.page()](#targetpage).
|
||||
|
||||
An array of all pages inside the Browser. In case of multiple browser contexts,
|
||||
the method will return an array with all the pages in all browser contexts.
|
||||
|
||||
#### browser.process()
|
||||
- returns: <?[ChildProcess]> Spawned browser process. Returns `null` if the browser instance was created with [`puppeteer.connect`](#puppeteerconnectoptions) method.
|
||||
|
||||
@ -652,6 +656,11 @@ The default browser context is the only non-incognito browser context.
|
||||
|
||||
Creates a new page in the browser context.
|
||||
|
||||
#### browserContext.pages()
|
||||
- returns: <[Promise]<[Array]<[Page]>>> Promise which resolves to an array of all open pages. Non visible pages, such as `"background_page"`, will not be listed here. You can find them using [target.page()](#targetpage).
|
||||
|
||||
An array of all pages inside the browser context.
|
||||
|
||||
#### browserContext.targets()
|
||||
- returns: <[Array]<[Target]>>
|
||||
|
||||
|
@ -183,12 +183,9 @@ class Browser extends EventEmitter {
|
||||
* @return {!Promise<!Array<!Puppeteer.Page>>}
|
||||
*/
|
||||
async pages() {
|
||||
const pages = await Promise.all(
|
||||
this.targets()
|
||||
.filter(target => target.type() === 'page')
|
||||
.map(target => target.page())
|
||||
);
|
||||
return pages.filter(page => !!page);
|
||||
const contextPages = await Promise.all(this.browserContexts().map(context => context.pages()));
|
||||
// Flatten array.
|
||||
return contextPages.reduce((acc, x) => acc.concat(x), []);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -250,6 +247,18 @@ class BrowserContext extends EventEmitter {
|
||||
return this._browser.targets().filter(target => target.browserContext() === this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {!Promise<!Array<!Puppeteer.Page>>}
|
||||
*/
|
||||
async pages() {
|
||||
const pages = await Promise.all(
|
||||
this.targets()
|
||||
.filter(target => target.type() === 'page')
|
||||
.map(target => target.page())
|
||||
);
|
||||
return pages.filter(page => !!page);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {boolean}
|
||||
*/
|
||||
|
@ -45,6 +45,7 @@ module.exports.addTests = function({testRunner, expect, puppeteer}) {
|
||||
const context = await browser.createIncognitoBrowserContext();
|
||||
await context.newPage();
|
||||
expect((await browser.pages()).length).toBe(3);
|
||||
expect((await context.pages()).length).toBe(1);
|
||||
|
||||
await context.close();
|
||||
expect((await browser.pages()).length).toBe(2);
|
||||
|
Loading…
Reference in New Issue
Block a user