Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix: do not stall waiting for pending navigations after beforeunload dismiss (#33834)

Dmitry Gozmanbe78e9e11f3d
diff --git a/packages/playwright-core/src/server/chromium/crPage.ts b/packages/playwright-core/src/server/chromium/crPage.ts
index f3f21121e5..bfad678f1c 100644
--- a/packages/playwright-core/src/server/chromium/crPage.ts
+++ b/packages/playwright-core/src/server/chromium/crPage.ts
@@ -846,6 +846,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 13096075f7..a6e2145ce4 100644
--- a/packages/playwright-core/src/server/webkit/wkPage.ts
+++ b/packages/playwright-core/src/server/webkit/wkPage.ts
@@ -611,6 +611,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 9f0982eea7..05134bd429 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(`<a href="${server.PREFIX}/frames/one-frame.html">click me</a>`);
+
+ 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');
+});