Skip to content

Commit

Permalink
feat(browsercontext): add BrowserContext.pages() method (#3003)
Browse files Browse the repository at this point in the history
  • Loading branch information
aslushnikov committed Jul 31, 2018
1 parent 25d7eff commit c018ff1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
9 changes: 9 additions & 0 deletions docs/api.md
Expand Up @@ -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)
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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]>>

Expand Down
21 changes: 15 additions & 6 deletions lib/Browser.js
Expand Up @@ -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), []);
}

/**
Expand Down Expand Up @@ -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}
*/
Expand Down
1 change: 1 addition & 0 deletions test/browsercontext.spec.js
Expand Up @@ -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);
Expand Down

0 comments on commit c018ff1

Please sign in to comment.