diff --git a/packages/playwright-core/src/server/chromium/crPage.ts b/packages/playwright-core/src/server/chromium/crPage.ts index 57ebc5fa69b8b..6d9dac039ad46 100644 --- a/packages/playwright-core/src/server/chromium/crPage.ts +++ b/packages/playwright-core/src/server/chromium/crPage.ts @@ -842,6 +842,9 @@ class FrameSession { event.type, event.message, async (accept: boolean, promptText?: string) => { + // TODO: this should actually be a CDP event that notifies about a cancelled navigation attempt. + if (this._isMainFrame() && event.type === 'beforeunload' && !accept) + this._page._frameManager.frameAbortedNavigation(this._page.mainFrame()._id, 'navigation cancelled by beforeunload dialog'); await this._client.send('Page.handleJavaScriptDialog', { accept, promptText }); }, event.defaultPrompt)); diff --git a/packages/playwright-core/src/server/webkit/wkPage.ts b/packages/playwright-core/src/server/webkit/wkPage.ts index 0bd82ed338342..b5e2281d5f539 100644 --- a/packages/playwright-core/src/server/webkit/wkPage.ts +++ b/packages/playwright-core/src/server/webkit/wkPage.ts @@ -612,6 +612,9 @@ export class WKPage implements PageDelegate { event.type as dialog.DialogType, event.message, async (accept: boolean, promptText?: string) => { + // TODO: this should actually be a RDP event that notifies about a cancelled navigation attempt. + if (event.type === 'beforeunload' && !accept) + this._page._frameManager.frameAbortedNavigation(this._page.mainFrame()._id, 'navigation cancelled by beforeunload dialog'); await this._pageProxySession.send('Dialog.handleJavaScriptDialog', { accept, promptText }); }, event.defaultPrompt)); diff --git a/tests/library/beforeunload.spec.ts b/tests/library/beforeunload.spec.ts index 9f0982eea7654..05134bd429332 100644 --- a/tests/library/beforeunload.spec.ts +++ b/tests/library/beforeunload.spec.ts @@ -104,3 +104,25 @@ it('should not stall on evaluate when dismissing beforeunload', async ({ page, s ]); }); +it('should not stall on click when dismissing beforeunload', async ({ page, server }) => { + it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/33806' }); + + await page.goto(server.EMPTY_PAGE); + await page.setContent(`click me`); + + await page.evaluate(() => { + window.onbeforeunload = () => false; + }); + page.on('dialog', async dialog => { + await dialog.dismiss(); + }); + + await page.getByRole('link').click({ noWaitAfter: true }); + await page.evaluate(() => { + window.onbeforeunload = null; + }); + + // This line should not timeout. + await page.getByRole('link').click({ timeout: 5000 }); + await expect(page).toHaveURL(server.PREFIX + '/frames/one-frame.html'); +});