File tree Expand file tree Collapse file tree 2 files changed +45
-2
lines changed Expand file tree Collapse file tree 2 files changed +45
-2
lines changed Original file line number Diff line number Diff line change 66
77import z from 'zod' ;
88
9+ import { logger } from '../logger.js' ;
10+
911import { ToolCategories } from './categories.js' ;
1012import { CLOSE_PAGE_ERROR , defineTool } from './ToolDefinition.js' ;
1113
@@ -195,12 +197,22 @@ export const handleDialog = defineTool({
195197
196198 switch ( request . params . action ) {
197199 case 'accept' : {
198- await dialog . accept ( request . params . promptText ) ;
200+ try {
201+ await dialog . accept ( request . params . promptText ) ;
202+ } catch ( err ) {
203+ // Likely already handled by the user outside of MCP.
204+ logger ( err ) ;
205+ }
199206 response . appendResponseLine ( 'Successfully accepted the dialog' ) ;
200207 break ;
201208 }
202209 case 'dismiss' : {
203- await dialog . dismiss ( ) ;
210+ try {
211+ await dialog . dismiss ( ) ;
212+ } catch ( err ) {
213+ // Likely already handled.
214+ logger ( err ) ;
215+ }
204216 response . appendResponseLine ( 'Successfully dismissed the dialog' ) ;
205217 break ;
206218 }
Original file line number Diff line number Diff line change 66import assert from 'node:assert' ;
77import { describe , it } from 'node:test' ;
88
9+ import type { Dialog } from 'puppeteer-core' ;
10+
911import {
1012 listPages ,
1113 newPage ,
@@ -265,5 +267,34 @@ describe('pages', () => {
265267 ) ;
266268 } ) ;
267269 } ) ;
270+ it ( 'can dismiss alread dismissed dialog dialogs' , async ( ) => {
271+ await withBrowser ( async ( response , context ) => {
272+ const page = context . getSelectedPage ( ) ;
273+ const dialogPromise = new Promise < Dialog > ( resolve => {
274+ page . on ( 'dialog' , dialog => {
275+ resolve ( dialog ) ;
276+ } ) ;
277+ } ) ;
278+ page . evaluate ( ( ) => {
279+ alert ( 'test' ) ;
280+ } ) ;
281+ const dialog = await dialogPromise ;
282+ await dialog . dismiss ( ) ;
283+ await handleDialog . handler (
284+ {
285+ params : {
286+ action : 'dismiss' ,
287+ } ,
288+ } ,
289+ response ,
290+ context ,
291+ ) ;
292+ assert . strictEqual ( context . getDialog ( ) , undefined ) ;
293+ assert . strictEqual (
294+ response . responseLines [ 0 ] ,
295+ 'Successfully dismissed the dialog' ,
296+ ) ;
297+ } ) ;
298+ } ) ;
268299 } ) ;
269300} ) ;
You can’t perform that action at this time.
0 commit comments