Skip to content

Commit

Permalink
fix: do not stall waiting for pending navigations after beforeunload …
Browse files Browse the repository at this point in the history
…dismiss (#33834)
  • Loading branch information
dgozman authored Dec 3, 2024
1 parent abf6916 commit be78e9e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/playwright-core/src/server/chromium/crPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
3 changes: 3 additions & 0 deletions packages/playwright-core/src/server/webkit/wkPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
22 changes: 22 additions & 0 deletions tests/library/beforeunload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

0 comments on commit be78e9e

Please sign in to comment.