From 974f82073f22cb20ed11b6225164fc816285a9e5 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Mon, 16 Dec 2019 18:33:36 -0700 Subject: [PATCH] feat(webkit): allow to run beforeunload when closing page (#264) --- src/webkit/Browser.ts | 5 +++-- src/webkit/FrameManager.ts | 4 +--- test/page.spec.js | 8 +++++--- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/webkit/Browser.ts b/src/webkit/Browser.ts index 9f05685990..42f6a38321 100644 --- a/src/webkit/Browser.ts +++ b/src/webkit/Browser.ts @@ -190,9 +190,10 @@ export class Browser extends EventEmitter implements BrowserInterface { target._didClose(); } - _closePage(page: Page) { + _closePage(page: Page, runBeforeUnload: boolean) { this._connection.send('Target.close', { - targetId: Target.fromPage(page)._targetId + targetId: Target.fromPage(page)._targetId, + runBeforeUnload }).catch(debugError); } diff --git a/src/webkit/FrameManager.ts b/src/webkit/FrameManager.ts index 9cb02ac80e..f0da9baa98 100644 --- a/src/webkit/FrameManager.ts +++ b/src/webkit/FrameManager.ts @@ -400,9 +400,7 @@ export class FrameManager implements PageDelegate { } async closePage(runBeforeUnload: boolean): Promise { - if (runBeforeUnload) - throw new Error('Not implemented'); - (this._page.browser() as Browser)._closePage(this._page); + (this._page.browser() as Browser)._closePage(this._page, runBeforeUnload); } getBoundingBoxForScreenshot(handle: dom.ElementHandle): Promise { diff --git a/test/page.spec.js b/test/page.spec.js index 9dd5af1cd1..c64abffd1b 100644 --- a/test/page.spec.js +++ b/test/page.spec.js @@ -40,7 +40,7 @@ module.exports.addTests = function({testRunner, expect, headless, playwright, FF await newPage.close(); expect(await browser.pages()).not.toContain(newPage); }); - it.skip(WEBKIT)('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 @@ -50,14 +50,16 @@ module.exports.addTests = function({testRunner, expect, headless, playwright, FF const dialog = await waitEvent(newPage, 'dialog'); expect(dialog.type()).toBe('beforeunload'); expect(dialog.defaultValue()).toBe(''); - if (CHROME || WEBKIT) + if (CHROME) expect(dialog.message()).toBe(''); + else if (WEBKIT) + expect(dialog.message()).toBe('Leave?'); 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.skip(WEBKIT)('should *not* run beforeunload by default', async({context, server}) => { + 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