Skip to content

Chrome DevTools for coding agents

fix: clear handled dialog in McpPage.getDialog() (#3241)

Nikolay Vitkov2ab93a3c0494
diff --git a/src/McpPage.ts b/src/McpPage.ts
index 47fe9f4e..fcd3a1b8 100644
--- a/src/McpPage.ts
+++ b/src/McpPage.ts
@@ -363,6 +363,9 @@ export class McpPage implements ContextPage {
}
getDialog(): Dialog | undefined {
+ if (this.#dialog?.handled) {
+ this.#dialog = undefined;
+ }
return this.#dialog;
}
@@ -379,9 +382,10 @@ export class McpPage implements ContextPage {
}
throwIfDialogOpen(): void {
- if (this.#dialog && !this.#dialog.handled) {
+ const dialog = this.getDialog();
+ if (dialog) {
throw new Error(
- `A dialog is open (${this.#dialog.type()}: ${this.#dialog.message()}).`,
+ `A dialog is open (${dialog.type()}: ${dialog.message()}).`,
);
}
}
diff --git a/tests/McpPage.test.ts b/tests/McpPage.test.ts
index faaea949..abf689b9 100644
--- a/tests/McpPage.test.ts
+++ b/tests/McpPage.test.ts
@@ -16,6 +16,7 @@ import type {JSONSchema7Definition} from '../src/third_party/index.js';
import {TextSnapshot} from '../src/TextSnapshot.js';
import type {TextSnapshotNode} from '../src/types.js';
import {
+ createMockDialog,
createMockMcpResponse,
createMockPuppeteerPage,
createMockPuppeteerTarget,
@@ -1739,4 +1740,32 @@ describe('McpPage', () => {
]);
});
});
+
+ describe('dialogs', () => {
+ it('returns the dialog and throws when unhandled', async () => {
+ const {mcpPage, pptrPage} = await createMcpPage();
+ const dialog = createMockDialog({type: 'alert', message: 'hello'});
+
+ pptrPage.emit('dialog', dialog);
+
+ assert.strictEqual(mcpPage.getDialog(), dialog);
+ assert.throws(() => mcpPage.throwIfDialogOpen(), {
+ message: 'A dialog is open (alert: hello).',
+ });
+ });
+
+ it('clears the dialog and does not throw when handled', async () => {
+ const {mcpPage, pptrPage} = await createMcpPage();
+ const dialog = createMockDialog({
+ type: 'alert',
+ message: 'hello',
+ handled: true,
+ });
+
+ pptrPage.emit('dialog', dialog);
+
+ assert.strictEqual(mcpPage.getDialog(), undefined);
+ assert.doesNotThrow(() => mcpPage.throwIfDialogOpen());
+ });
+ });
});
diff --git a/tests/mocks.ts b/tests/mocks.ts
index e0a33345..76a4ba81 100644
--- a/tests/mocks.ts
+++ b/tests/mocks.ts
@@ -466,11 +466,16 @@ export function createMockMcpPage(
}
export function createMockDialog(
- options: {type?: Protocol.Page.DialogType; message?: string} = {},
+ options: {
+ type?: Protocol.Page.DialogType;
+ message?: string;
+ handled?: boolean;
+ } = {},
): sinon.SinonStubbedInstance<Dialog> {
const dialog = sinon.createStubInstance(Dialog);
dialog.type.returns(options.type ?? 'alert');
dialog.message.returns(options.message ?? '');
+ sinon.stub(dialog, 'handled').get(() => options.handled ?? false);
return dialog;
}
diff --git a/tests/tools/pages.test.js.snapshot b/tests/tools/pages.test.js.snapshot
index 7ed6a21a..6f9997e0 100644
--- a/tests/tools/pages.test.js.snapshot
+++ b/tests/tools/pages.test.js.snapshot
@@ -35,7 +35,7 @@ exports[`pages > list_pages > when dialog is open 1`] = `
`;
exports[`pages > navigate_page > when dialog is open 1`] = `
-{"content":[{"type":"text","text":"Successfully navigated to data:text/html,<div>Navigated</div>.\\n# Open dialog\\nalert: test dialog.\\nCall handle_dialog to handle it before continuing.\\n## Pages\\n1: data:text/html,<div>Navigated</div> [selected]"}],"structuredContent":{"message":"Successfully navigated to data:text/html,<div>Navigated</div>.","dialog":{"type":"alert","message":"test dialog","defaultValue":""},"pages":[{"id":1,"url":"data:text/html,<div>Navigated</div>","title":"","selected":true}]}}
+{"content":[{"type":"text","text":"Successfully navigated to data:text/html,<div>Navigated</div>.\\n## Pages\\n1: data:text/html,<div>Navigated</div> [selected]"}],"structuredContent":{"message":"Successfully navigated to data:text/html,<div>Navigated</div>.","pages":[{"id":1,"url":"data:text/html,<div>Navigated</div>","title":"","selected":true}]}}
`;
exports[`pages > new_page with isolatedContext > when dialog is open 1`] = `
diff --git a/tests/tools/pages.test.ts b/tests/tools/pages.test.ts
index afbeab27..01fce61a 100644
--- a/tests/tools/pages.test.ts
+++ b/tests/tools/pages.test.ts
@@ -1696,7 +1696,7 @@ describe('pages', () => {
await evalPromise;
});
});
- it('can dismiss already dismissed dialog dialogs', async () => {
+ it('throws when the dialog was already dismissed', async () => {
await withMcpContext(async (response, context, args) => {
const page = context.getSelectedMcpPage().pptrPage;
const dialogPromise = new Promise<Dialog>(resolve => {
@@ -1709,20 +1709,19 @@ describe('pages', () => {
});
const dialog = await dialogPromise;
await dialog.dismiss();
- await handleDialog(args).handler(
- {
- params: {
- action: 'dismiss',
- },
- page: context.getSelectedMcpPage(),
- },
- response,
- context,
- );
assert.strictEqual(context.getSelectedMcpPage().getDialog(), undefined);
- assert.strictEqual(
- response.responseLines[0],
- 'Successfully dismissed the dialog',
+ await assert.rejects(
+ handleDialog(args).handler(
+ {
+ params: {
+ action: 'dismiss',
+ },
+ page: context.getSelectedMcpPage(),
+ },
+ response,
+ context,
+ ),
+ {message: 'No open dialog found'},
);
await evalPromise;
});