Skip to content

Commit 03a996f

Browse files
authored
fix(route): don't get stalled by "runBeforeUnload" (microsoft#38748)
1 parent f141c88 commit 03a996f

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

packages/playwright-core/src/client/page.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,8 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
631631

632632
async close(options: { runBeforeUnload?: boolean, reason?: string } = {}) {
633633
this._closeReason = options.reason;
634-
this._closeWasCalled = true;
634+
if (!options.runBeforeUnload)
635+
this._closeWasCalled = true;
635636
try {
636637
if (this._ownedContext)
637638
await this._ownedContext.close();

tests/page/page-route.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,3 +1069,25 @@ it('should be able to intercept every navigation to a page controlled by service
10691069
await page.goto(URL);
10701070
expect(interceptions).toBe(2);
10711071
});
1072+
1073+
it('does not get stalled by beforeUnload', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/38731' } }, async ({ page, server }) => {
1074+
await page.goto(server.HELLO_WORLD);
1075+
1076+
await page.evaluate(() => {
1077+
window.addEventListener('beforeunload', event => {
1078+
event.preventDefault();
1079+
});
1080+
});
1081+
page.on('dialog', dialog => dialog.dismiss());
1082+
1083+
// We have to interact with a page so that 'beforeunload' handlers
1084+
// fire.
1085+
await page.click('body');
1086+
1087+
await page.route('**/api', route => route.fulfill({ status: 200, body: 'ok' }));
1088+
await page.evaluate(async () => fetch(new URL('/api', window.location.href)));
1089+
1090+
await page.close({ runBeforeUnload: true });
1091+
1092+
await page.evaluate(async () => fetch(new URL('/api', window.location.href)));
1093+
});

0 commit comments

Comments
 (0)