Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
fix(library): don't dirty page state when calling page.close() (#37608)
Adam Gastineau6fa48c17db6b
diff --git a/packages/playwright-core/src/server/page.ts b/packages/playwright-core/src/server/page.tsindex 5df5bf4614..fd5a9fa9a2 100644--- a/packages/playwright-core/src/server/page.ts+++ b/packages/playwright-core/src/server/page.ts@@ -755,7 +755,9 @@ export class Page extends SdkObject {this.closeReason = options.reason;const runBeforeUnload = !!options.runBeforeUnload;if (this._closedState !== 'closing') {- this._closedState = 'closing';+ // If runBeforeUnload is true, we don't know if we will close, so don't modify the state+ if (!runBeforeUnload)+ this._closedState = 'closing';// This might throw if the browser context containing the page closes// while we are trying to close the page.await this.delegate.closePage(runBeforeUnload).catch(e => debugLogger.log('error', e));diff --git a/tests/library/beforeunload.spec.ts b/tests/library/beforeunload.spec.tsindex 05134bd429..e39fdd2e32 100644--- a/tests/library/beforeunload.spec.ts+++ b/tests/library/beforeunload.spec.ts@@ -126,3 +126,61 @@ it('should not stall on click when dismissing beforeunload', async ({ page, servawait page.getByRole('link').click({ timeout: 5000 });await expect(page).toHaveURL(server.PREFIX + '/frames/one-frame.html');});++it('should support dismissing the dialog multiple times', async ({ page, server }) => {+ await page.goto(server.PREFIX + '/beforeunload.html');++ // We have to interact with a page so that 'beforeunload' handlers+ // fire (in Firefox).+ await page.click('body');++ const [dialog] = await Promise.all([+ page.waitForEvent('dialog'),+ page.close({ runBeforeUnload: true })+ ]);++ await dialog.dismiss();++ const [dialog2] = await Promise.all([+ page.waitForEvent('dialog'),+ page.close({ runBeforeUnload: true })+ ]);++ await dialog2.dismiss();+});++it('should support closing the page after a previous dismiss', async ({ page, server }) => {+ await page.goto(server.PREFIX + '/beforeunload.html');++ // We have to interact with a page so that 'beforeunload' handlers+ // fire (in Firefox).+ await page.click('body');++ const [dialog] = await Promise.all([+ page.waitForEvent('dialog'),+ page.close({ runBeforeUnload: true })+ ]);++ await dialog.dismiss();++ await page.close();+ await expect(page.isClosed()).toBe(true);+});++it('should support closing the page via a subsequent onbeforeunload dialog', async ({ page, server }) => {+ await page.goto(server.PREFIX + '/beforeunload.html');++ // We have to interact with a page so that 'beforeunload' handlers+ // fire (in Firefox).+ await page.click('body');++ const [dialog] = await Promise.all([+ page.waitForEvent('dialog'),+ page.close({ runBeforeUnload: true })+ ]);++ await dialog.accept();++ await page.waitForEvent('close');+ await expect(page.isClosed()).toBe(true);+});