feat(firefox): support running beforeunload hooks when closing (#4003)

This commit is contained in:
Andrey Lushnikov 2019-02-13 22:16:12 -08:00 committed by GitHub
parent e3b76b2beb
commit 0b40d04b99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 9 deletions

View File

@ -549,8 +549,11 @@ class Page extends EventEmitter {
return await this._frameManager.mainFrame().select(selector, ...values); return await this._frameManager.mainFrame().select(selector, ...values);
} }
async close() { async close(options = {}) {
await this._session.send('Browser.closePage' ); const {
runBeforeUnload = false,
} = options;
await this._session.send('Browser.closePage', { runBeforeUnload });
} }
async content() { async content() {

View File

@ -104,7 +104,7 @@ pref("datareporting.policy.dataSubmissionPolicyAccepted", false);
pref("datareporting.policy.dataSubmissionPolicyBypassNotification", true); pref("datareporting.policy.dataSubmissionPolicyBypassNotification", true);
// Automatically unload beforeunload alerts // Automatically unload beforeunload alerts
pref("dom.disable_beforeunload", true); pref("dom.disable_beforeunload", false);
// Disable popup-blocker // Disable popup-blocker
pref("dom.disable_open_during_load", false); pref("dom.disable_open_during_load", false);

View File

@ -9,7 +9,7 @@
"node": ">=8.9.4" "node": ">=8.9.4"
}, },
"puppeteer": { "puppeteer": {
"firefox_revision": "0647e24cc0b90c07c8ddb32e63ce333839329527" "firefox_revision": "4ba5b441257d5938d032d09fc09e45ea9d8f2e3a"
}, },
"scripts": { "scripts": {
"install": "node install.js", "install": "node install.js",

View File

@ -1,5 +1,10 @@
<div>beforeunload demo.</div>
<script> <script>
window.addEventListener('beforeunload', event => { window.addEventListener('beforeunload', event => {
// Chrome way.
event.returnValue = 'Leave?'; event.returnValue = 'Leave?';
// Firefox way.
event.preventDefault();
}); });
</script> </script>

View File

@ -48,19 +48,30 @@ module.exports.addTests = function({testRunner, expect, headless, Errors, Device
await newPage.close(); await newPage.close();
expect(await browser.pages()).not.toContain(newPage); 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(); const newPage = await context.newPage();
await newPage.goto(server.PREFIX + '/beforeunload.html'); await newPage.goto(server.PREFIX + '/beforeunload.html');
// We have to interact with a page so that 'beforeunload' handlers // We have to interact with a page so that 'beforeunload' handlers
// fire. // fire.
await newPage.click('body'); await newPage.click('body');
newPage.close({ runBeforeUnload: true }); const pageClosingPromise = newPage.close({ runBeforeUnload: true });
const dialog = await waitEvent(newPage, 'dialog'); const dialog = await waitEvent(newPage, 'dialog');
expect(dialog.type()).toBe('beforeunload'); expect(dialog.type()).toBe('beforeunload');
expect(dialog.defaultValue()).toBe(''); expect(dialog.defaultValue()).toBe('');
if (CHROME)
expect(dialog.message()).toBe(''); expect(dialog.message()).toBe('');
dialog.accept(); else
await waitEvent(newPage, 'close'); 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}) => { it('should set the page close state', async({context}) => {
const newPage = await context.newPage(); const newPage = await context.newPage();