mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
feat(Page): add option to run 'beforeunload' when closing the page (#2478)
Today, `page.close()` method doesn't run page's beforeunload listeners. This way users can be sure that `page.close()` actually closes the page. This patch adds an optional `runBeforeUnload` option to the `page.close()` method that would run beforeunload listeners. Note: running beforeunload handlers might cancel page closing. Fixes #2386.
This commit is contained in:
5
test/assets/beforeunload.html
Normal file
5
test/assets/beforeunload.html
Normal file
@@ -0,0 +1,5 @@
|
||||
<script>
|
||||
window.addEventListener('beforeunload', event => {
|
||||
event.returnValue = 'Leave?';
|
||||
});
|
||||
</script>
|
||||
@@ -40,6 +40,20 @@ module.exports.addTests = function({testRunner, expect, puppeteer, DeviceDescrip
|
||||
await newPage.close();
|
||||
expect(await browser.pages()).not.toContain(newPage);
|
||||
});
|
||||
it('should run beforeunload if asked for', async({browser, server}) => {
|
||||
const newPage = await browser.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 dialog = await waitEvent(newPage, 'dialog');
|
||||
expect(dialog.type()).toBe('beforeunload');
|
||||
expect(dialog.defaultValue()).toBe('');
|
||||
expect(dialog.message()).toBe('');
|
||||
dialog.accept();
|
||||
await waitEvent(newPage, 'close');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Page.Events.error', function() {
|
||||
|
||||
Reference in New Issue
Block a user