From cfefd611b80561f7be72c438e6de0e64f8a88515 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Fri, 8 Feb 2019 19:21:54 -0800 Subject: [PATCH] feat(firefox): support target.opener() and page.on('popup') (#3966) --- experimental/puppeteer-firefox/lib/Browser.js | 23 ++++++++++++++++--- experimental/puppeteer-firefox/lib/Events.js | 1 + experimental/puppeteer-firefox/package.json | 2 +- test/page.spec.js | 2 +- test/target.spec.js | 2 +- 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/experimental/puppeteer-firefox/lib/Browser.js b/experimental/puppeteer-firefox/lib/Browser.js index 18e05d0d..a14ee71c 100644 --- a/experimental/puppeteer-firefox/lib/Browser.js +++ b/experimental/puppeteer-firefox/lib/Browser.js @@ -158,10 +158,18 @@ class Browser extends EventEmitter { return Array.from(this._pageTargets.values()); } - _onTabOpened({pageId, url, browserContextId}) { + async _onTabOpened({pageId, url, browserContextId, openerId}) { const context = browserContextId ? this._contexts.get(browserContextId) : this._defaultContext; - const target = new Target(this._connection, this, context, pageId, url); + const opener = openerId ? this._pageTargets.get(openerId) : null; + const target = new Target(this._connection, this, context, pageId, url, opener); this._pageTargets.set(pageId, target); + if (opener && opener._pagePromise) { + const openerPage = await opener._pagePromise; + if (openerPage.listenerCount(Events.Page.Popup)) { + const popupPage = await target.page(); + openerPage.emit(Events.Page.Popup, popupPage); + } + } this.emit(Events.Browser.TargetCreated, target); context.emit(Events.BrowserContext.TargetCreated, target); } @@ -194,8 +202,9 @@ class Target { * @param {!BrowserContext} context * @param {string} pageId * @param {string} url + * @param {?Target} opener */ - constructor(connection, browser, context, pageId, url) { + constructor(connection, browser, context, pageId, url, opener) { this._browser = browser; this._context = context; this._connection = connection; @@ -203,6 +212,14 @@ class Target { /** @type {?Promise} */ this._pagePromise = null; this._url = url; + this._opener = opener; + } + + /** + * @return {?Target} + */ + opener() { + return this._opener; } /** diff --git a/experimental/puppeteer-firefox/lib/Events.js b/experimental/puppeteer-firefox/lib/Events.js index f00c9032..5646db63 100644 --- a/experimental/puppeteer-firefox/lib/Events.js +++ b/experimental/puppeteer-firefox/lib/Events.js @@ -9,6 +9,7 @@ const Events = { FrameNavigated: 'framenavigated', Load: 'load', PageError: 'pageerror', + Popup: 'popup', }, Browser: { TargetCreated: 'targetcreated', diff --git a/experimental/puppeteer-firefox/package.json b/experimental/puppeteer-firefox/package.json index 3cd64fc4..a45db348 100644 --- a/experimental/puppeteer-firefox/package.json +++ b/experimental/puppeteer-firefox/package.json @@ -9,7 +9,7 @@ "node": ">=8.9.4" }, "puppeteer": { - "firefox_revision": "ed8e119ec1279c3db3638e90e910edb3816e0280" + "firefox_revision": "9702d3d2dd86550e385fadec66eb28a5ff2b1322" }, "scripts": { "install": "node install.js", diff --git a/test/page.spec.js b/test/page.spec.js index 20b5fe0d..59717612 100644 --- a/test/page.spec.js +++ b/test/page.spec.js @@ -102,7 +102,7 @@ module.exports.addTests = function({testRunner, expect, headless, Errors, Device }); }); - describe_fails_ffox('Page.Events.Popup', function() { + describe('Page.Events.Popup', function() { it('should work', async({page}) => { const [popup] = await Promise.all([ new Promise(x => page.once('popup', x)), diff --git a/test/target.spec.js b/test/target.spec.js index ea042b41..818eef48 100644 --- a/test/target.spec.js +++ b/test/target.spec.js @@ -132,7 +132,7 @@ module.exports.addTests = function({testRunner, expect, puppeteer, Errors}) { // Cleanup. await newPage.close(); }); - it_fails_ffox('should have an opener', async({page, server, context}) => { + it('should have an opener', async({page, server, context}) => { await page.goto(server.EMPTY_PAGE); const [createdTarget] = await Promise.all([ new Promise(fulfill => context.once('targetcreated', target => fulfill(target))),