diff --git a/docs/api.md b/docs/api.md index 4568a2ba6ab..344feb38b71 100644 --- a/docs/api.md +++ b/docs/api.md @@ -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: 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]>> diff --git a/lib/Browser.js b/lib/Browser.js index e1ff044bfa8..e6c437ad4a9 100644 --- a/lib/Browser.js +++ b/lib/Browser.js @@ -183,12 +183,9 @@ class Browser extends EventEmitter { * @return {!Promise>} */ 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>} + */ + 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} */ diff --git a/test/browsercontext.spec.js b/test/browsercontext.spec.js index 19626bd91d8..d14b4cfdf6e 100644 --- a/test/browsercontext.spec.js +++ b/test/browsercontext.spec.js @@ -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);