From 0ff7d51ccdacea1d99c2243d89010e126c2f6d4c Mon Sep 17 00:00:00 2001 From: Holger Benl Date: Wed, 25 Mar 2026 09:47:18 +0100 Subject: [PATCH] chore: swallow TargetClosedErrors in dialog.dismiss() --- packages/playwright-core/src/client/dialog.ts | 9 ++++++++- tests/library/page-close.spec.ts | 10 ---------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/packages/playwright-core/src/client/dialog.ts b/packages/playwright-core/src/client/dialog.ts index 52722cecfd61f..98a41291cc607 100644 --- a/packages/playwright-core/src/client/dialog.ts +++ b/packages/playwright-core/src/client/dialog.ts @@ -16,6 +16,7 @@ import { ChannelOwner } from './channelOwner'; import { Page } from './page'; +import { isTargetClosedError } from './errors'; import type * as api from '../../types/types'; import type * as channels from '@protocol/channels'; @@ -56,6 +57,12 @@ export class Dialog extends ChannelOwner implements api. } async dismiss() { - await this._channel.dismiss(); + try { + await this._channel.dismiss(); + } catch (e) { + if (isTargetClosedError(e)) + return; + throw e; + } } } diff --git a/tests/library/page-close.spec.ts b/tests/library/page-close.spec.ts index 46832decb39c5..5569dfbcad11e 100644 --- a/tests/library/page-close.spec.ts +++ b/tests/library/page-close.spec.ts @@ -27,16 +27,6 @@ test('should close page with active dialog', async ({ page }) => { await page.close(); }); -test('should not accept dialog after close', async ({ page, mode }) => { - test.fixme(mode.startsWith('service2'), 'Times out'); - const promise = page.waitForEvent('dialog'); - page.evaluate(() => alert()).catch(() => {}); - const dialog = await promise; - await page.close(); - const e = await dialog.dismiss().catch(e => e); - expect(e.message).toContain('Target page, context or browser has been closed'); -}); - test('expect should not print timed out error message when page closes', async ({ page }) => { await page.setContent('
Text content
'); const [error] = await Promise.all([