Skip to content

Chrome DevTools for coding agents

fix: do not error if the dialog was already handled (#208)

Alex Rudenkod9f77f85098f
diff --git a/src/tools/pages.ts b/src/tools/pages.ts
index c5ca2a09..c9962e7c 100644
--- a/src/tools/pages.ts
+++ b/src/tools/pages.ts
@@ -6,6 +6,8 @@
import z from 'zod';
+import {logger} from '../logger.js';
+
import {ToolCategories} from './categories.js';
import {CLOSE_PAGE_ERROR, defineTool} from './ToolDefinition.js';
@@ -195,12 +197,22 @@ export const handleDialog = defineTool({
switch (request.params.action) {
case 'accept': {
- await dialog.accept(request.params.promptText);
+ try {
+ await dialog.accept(request.params.promptText);
+ } catch (err) {
+ // Likely already handled by the user outside of MCP.
+ logger(err);
+ }
response.appendResponseLine('Successfully accepted the dialog');
break;
}
case 'dismiss': {
- await dialog.dismiss();
+ try {
+ await dialog.dismiss();
+ } catch (err) {
+ // Likely already handled.
+ logger(err);
+ }
response.appendResponseLine('Successfully dismissed the dialog');
break;
}
diff --git a/tests/tools/pages.test.ts b/tests/tools/pages.test.ts
index 07ddd471..bee2e40c 100644
--- a/tests/tools/pages.test.ts
+++ b/tests/tools/pages.test.ts
@@ -6,6 +6,8 @@
import assert from 'node:assert';
import {describe, it} from 'node:test';
+import type {Dialog} from 'puppeteer-core';
+
import {
listPages,
newPage,
@@ -265,5 +267,34 @@ describe('pages', () => {
);
});
});
+ it('can dismiss alread dismissed dialog dialogs', async () => {
+ await withBrowser(async (response, context) => {
+ const page = context.getSelectedPage();
+ const dialogPromise = new Promise<Dialog>(resolve => {
+ page.on('dialog', dialog => {
+ resolve(dialog);
+ });
+ });
+ page.evaluate(() => {
+ alert('test');
+ });
+ const dialog = await dialogPromise;
+ await dialog.dismiss();
+ await handleDialog.handler(
+ {
+ params: {
+ action: 'dismiss',
+ },
+ },
+ response,
+ context,
+ );
+ assert.strictEqual(context.getDialog(), undefined);
+ assert.strictEqual(
+ response.responseLines[0],
+ 'Successfully dismissed the dialog',
+ );
+ });
+ });
});
});