Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/playwright-core/src/client/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,8 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page

async close(options: { runBeforeUnload?: boolean, reason?: string } = {}) {
this._closeReason = options.reason;
this._closeWasCalled = true;
if (!options.runBeforeUnload)
this._closeWasCalled = true;
try {
if (this._ownedContext)
await this._ownedContext.close();
Expand Down
22 changes: 22 additions & 0 deletions tests/page/page-route.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1069,3 +1069,25 @@ it('should be able to intercept every navigation to a page controlled by service
await page.goto(URL);
expect(interceptions).toBe(2);
});

it('does not get stalled by beforeUnload', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/38731' } }, async ({ page, server }) => {
await page.goto(server.HELLO_WORLD);

await page.evaluate(() => {
window.addEventListener('beforeunload', event => {
event.preventDefault();
});
});
page.on('dialog', dialog => dialog.dismiss());

// We have to interact with a page so that 'beforeunload' handlers
// fire.
await page.click('body');

await page.route('**/api', route => route.fulfill({ status: 200, body: 'ok' }));
await page.evaluate(async () => fetch(new URL('/api', window.location.href)));

await page.close({ runBeforeUnload: true });

await page.evaluate(async () => fetch(new URL('/api', window.location.href)));
});
Loading