feat(target): add support for target.page for 'backgroud_page' (#2600)

This patch teaches `target.page()` method to attach to extension background pages.

Fixes #2438
This commit is contained in:
Yaniv Efraim 2018-06-14 23:58:51 +03:00 committed by Andrey Lushnikov
parent cd8d750628
commit 38f112f395
4 changed files with 16 additions and 4 deletions

View File

@ -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: <?[ChildProcess]> 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]<?[Page]>>
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]>

View File

@ -183,7 +183,11 @@ class Browser extends EventEmitter {
* @return {!Promise<!Array<!Puppeteer.Page>>}
*/
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);
}

View File

@ -38,7 +38,7 @@ class Target {
* @return {!Promise<?Page>}
*/
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));
}

View File

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