feat(firefox): support target.opener() and page.on('popup') (#3966)

This commit is contained in:
Andrey Lushnikov 2019-02-08 19:21:54 -08:00 committed by GitHub
parent 0cf6ab68e7
commit cfefd611b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 6 deletions

View File

@ -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<!Page>} */
this._pagePromise = null;
this._url = url;
this._opener = opener;
}
/**
* @return {?Target}
*/
opener() {
return this._opener;
}
/**

View File

@ -9,6 +9,7 @@ const Events = {
FrameNavigated: 'framenavigated',
Load: 'load',
PageError: 'pageerror',
Popup: 'popup',
},
Browser: {
TargetCreated: 'targetcreated',

View File

@ -9,7 +9,7 @@
"node": ">=8.9.4"
},
"puppeteer": {
"firefox_revision": "ed8e119ec1279c3db3638e90e910edb3816e0280"
"firefox_revision": "9702d3d2dd86550e385fadec66eb28a5ff2b1322"
},
"scripts": {
"install": "node install.js",

View File

@ -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)),

View File

@ -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))),