diff --git a/docs/api.md b/docs/api.md index 415b0b1a..435cbb85 100644 --- a/docs/api.md +++ b/docs/api.md @@ -525,7 +525,7 @@ Disconnects Puppeteer from the browser, but leaves the Chromium process running. Promise which resolves to a new [Page] object. The [Page] is created in a default browser context. #### browser.pages() -- returns: <[Promise]<[Array]<[Page]>>> Promise which resolves to an array of all open 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). #### browser.process() - returns: Spawned browser process. Returns `null` if the browser instance was created with [`puppeteer.connect`](#puppeteerconnectoptions) method. @@ -2847,7 +2847,7 @@ Get the target that opened this target. Top-level targets return `null`. #### target.page() - returns: <[Promise]> -If the target is not of type `"page"`, returns `null`. +If the target is not of type `"page"` or `"background_page"`, returns `null`. #### target.type() - returns: <[string]> diff --git a/lib/Browser.js b/lib/Browser.js index 5fccaf95..e1ff044b 100644 --- a/lib/Browser.js +++ b/lib/Browser.js @@ -183,7 +183,11 @@ class Browser extends EventEmitter { * @return {!Promise>} */ async pages() { - const pages = await Promise.all(this.targets().map(target => target.page())); + const pages = await Promise.all( + this.targets() + .filter(target => target.type() === 'page') + .map(target => target.page()) + ); return pages.filter(page => !!page); } diff --git a/lib/Target.js b/lib/Target.js index ae76d382..8d15b241 100644 --- a/lib/Target.js +++ b/lib/Target.js @@ -38,7 +38,7 @@ class Target { * @return {!Promise} */ async page() { - if (this._targetInfo.type === 'page' && !this._pagePromise) { + if ((this._targetInfo.type === 'page' || this._targetInfo.type === 'background_page') && !this._pagePromise) { this._pagePromise = this._sessionFactory() .then(client => Page.create(client, this, this._ignoreHTTPSErrors, this._setDefaultViewport, this._screenshotTaskQueue)); } diff --git a/test/headful.spec.js b/test/headful.spec.js index f2cb3482..ad8b8d62 100644 --- a/test/headful.spec.js +++ b/test/headful.spec.js @@ -51,6 +51,14 @@ module.exports.addTests = function({testRunner, expect, PROJECT_ROOT, defaultBro await browserWithExtension.close(); expect(backgroundPageTarget).toBeTruthy(); }); + it('target.page() should return a background_page', async({browser}) => { + const browserWithExtension = await puppeteer.launch(extensionOptions); + const targets = await browserWithExtension.targets(); + const backgroundPageTarget = targets.find(target => target.type() === 'background_page'); + const page = await backgroundPageTarget.page(); + expect(await page.evaluate(() => 2 * 3)).toBe(6); + await browserWithExtension.close(); + }); it('should have default url when launching browser', async function() { const browser = await puppeteer.launch(extensionOptions); const pages = (await browser.pages()).map(page => page.url());