feat(browsercontext): add BrowserContext.pages() method (#3003)

This commit is contained in:
Andrey Lushnikov 2018-07-31 13:24:29 -07:00 committed by GitHub
parent 25d7eff374
commit c018ff1555
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 6 deletions

View File

@ -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]>>

View File

@ -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}
*/

View File

@ -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);