Skip to content

Commit

Permalink
feat(firefox): support running beforeunload hooks when closing (#4003)
Browse files Browse the repository at this point in the history
  • Loading branch information
aslushnikov committed Feb 14, 2019
1 parent e3b76b2 commit 0b40d04
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
7 changes: 5 additions & 2 deletions experimental/puppeteer-firefox/lib/Page.js
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion experimental/puppeteer-firefox/misc/puppeteer.cfg
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion experimental/puppeteer-firefox/package.json
Expand Up @@ -9,7 +9,7 @@
"node": ">=8.9.4"
},
"puppeteer": {
"firefox_revision": "0647e24cc0b90c07c8ddb32e63ce333839329527"
"firefox_revision": "4ba5b441257d5938d032d09fc09e45ea9d8f2e3a"
},
"scripts": {
"install": "node install.js",
Expand Down
5 changes: 5 additions & 0 deletions test/assets/beforeunload.html
@@ -1,5 +1,10 @@
<div>beforeunload demo.</div>
<script>
window.addEventListener('beforeunload', event => {
// Chrome way.
event.returnValue = 'Leave?';
// Firefox way.
event.preventDefault();
});
</script>

21 changes: 16 additions & 5 deletions test/page.spec.js
Expand Up @@ -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();
Expand Down

0 comments on commit 0b40d04

Please sign in to comment.