From 0b40d04b992085b484cc19ea3b24209713c66839 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Wed, 13 Feb 2019 22:16:12 -0800 Subject: [PATCH] feat(firefox): support running beforeunload hooks when closing (#4003) --- experimental/puppeteer-firefox/lib/Page.js | 7 +++++-- .../puppeteer-firefox/misc/puppeteer.cfg | 2 +- experimental/puppeteer-firefox/package.json | 2 +- test/assets/beforeunload.html | 5 +++++ test/page.spec.js | 21 ++++++++++++++----- 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/experimental/puppeteer-firefox/lib/Page.js b/experimental/puppeteer-firefox/lib/Page.js index b9d079de5f4..e3bf288956b 100644 --- a/experimental/puppeteer-firefox/lib/Page.js +++ b/experimental/puppeteer-firefox/lib/Page.js @@ -549,8 +549,11 @@ class Page extends EventEmitter { return await this._frameManager.mainFrame().select(selector, ...values); } - async close() { - await this._session.send('Browser.closePage' ); + async close(options = {}) { + const { + runBeforeUnload = false, + } = options; + await this._session.send('Browser.closePage', { runBeforeUnload }); } async content() { diff --git a/experimental/puppeteer-firefox/misc/puppeteer.cfg b/experimental/puppeteer-firefox/misc/puppeteer.cfg index 234a5a325bc..2d886e401dc 100644 --- a/experimental/puppeteer-firefox/misc/puppeteer.cfg +++ b/experimental/puppeteer-firefox/misc/puppeteer.cfg @@ -104,7 +104,7 @@ pref("datareporting.policy.dataSubmissionPolicyAccepted", false); pref("datareporting.policy.dataSubmissionPolicyBypassNotification", true); // Automatically unload beforeunload alerts -pref("dom.disable_beforeunload", true); +pref("dom.disable_beforeunload", false); // Disable popup-blocker pref("dom.disable_open_during_load", false); diff --git a/experimental/puppeteer-firefox/package.json b/experimental/puppeteer-firefox/package.json index 4652ebb84b0..1dd4b3166c8 100644 --- a/experimental/puppeteer-firefox/package.json +++ b/experimental/puppeteer-firefox/package.json @@ -9,7 +9,7 @@ "node": ">=8.9.4" }, "puppeteer": { - "firefox_revision": "0647e24cc0b90c07c8ddb32e63ce333839329527" + "firefox_revision": "4ba5b441257d5938d032d09fc09e45ea9d8f2e3a" }, "scripts": { "install": "node install.js", diff --git a/test/assets/beforeunload.html b/test/assets/beforeunload.html index 6c3075b475e..3cef6763f39 100644 --- a/test/assets/beforeunload.html +++ b/test/assets/beforeunload.html @@ -1,5 +1,10 @@ +
beforeunload demo.
+ diff --git a/test/page.spec.js b/test/page.spec.js index da437162de0..84c7d4805b3 100644 --- a/test/page.spec.js +++ b/test/page.spec.js @@ -48,19 +48,30 @@ module.exports.addTests = function({testRunner, expect, headless, Errors, Device await newPage.close(); expect(await browser.pages()).not.toContain(newPage); }); - it_fails_ffox('should run beforeunload if asked for', async({context, server}) => { + it('should run beforeunload if asked for', async({context, server}) => { const newPage = await context.newPage(); await newPage.goto(server.PREFIX + '/beforeunload.html'); // We have to interact with a page so that 'beforeunload' handlers // fire. await newPage.click('body'); - newPage.close({ runBeforeUnload: true }); + const pageClosingPromise = newPage.close({ runBeforeUnload: true }); const dialog = await waitEvent(newPage, 'dialog'); expect(dialog.type()).toBe('beforeunload'); expect(dialog.defaultValue()).toBe(''); - expect(dialog.message()).toBe(''); - dialog.accept(); - await waitEvent(newPage, 'close'); + if (CHROME) + expect(dialog.message()).toBe(''); + else + expect(dialog.message()).toBe('This page is asking you to confirm that you want to leave - data you have entered may not be saved.'); + await dialog.accept(); + await pageClosingPromise; + }); + it('should *not* run beforeunload by default', async({context, server}) => { + const newPage = await context.newPage(); + await newPage.goto(server.PREFIX + '/beforeunload.html'); + // We have to interact with a page so that 'beforeunload' handlers + // fire. + await newPage.click('body'); + await newPage.close(); }); it('should set the page close state', async({context}) => { const newPage = await context.newPage();