@@ -153,13 +153,16 @@ class PlaywrightServer {
153153 this . browsers = new Map ( ) ;
154154 this . contexts = new Map ( ) ;
155155 this . pages = new Map ( ) ;
156+ this . pageContexts = new Map ( ) ;
156157 this . responses = new Map ( ) ;
157158 this . browserCounter = 0 ;
158159 this . contextCounter = 0 ;
159160 this . pageCounter = 0 ;
160161 this . responseCounter = 0 ;
161162 this . routes = new Map ( ) ;
162163 this . routeCounter = 0 ;
164+ this . contextThrottling = new Map ( ) ;
165+ this . dialogs = new Map ( ) ;
163166 this . elementHandleCounter = 0 ;
164167 this . elementHandles = new Map ( ) ;
165168 }
@@ -222,6 +225,9 @@ class PlaywrightServer {
222225 if ( actionPrefix === 'route' ) {
223226 return await this . handleRouteCommand ( command , actionMethod ) ;
224227 }
228+ if ( actionPrefix === 'response' ) {
229+ return await this . handleResponseCommand ( command , actionMethod ) ;
230+ }
225231 if ( actionPrefix === 'mouse' ) {
226232 return await this . handleMouseCommand ( command , actionMethod ) ;
227233 }
@@ -289,10 +295,22 @@ class PlaywrightServer {
289295 case 'stopTracing' :
290296 await context . tracing . stop ( { path : command . path } ) ;
291297 return { success : true } ;
298+ case 'waitForEvent' :
299+ await context . waitForEvent ( command . event , { timeout : command . timeout } ) ;
300+ return { success : true } ;
301+ case 'setNetworkThrottling' :
302+ if ( command . throttling && typeof command . throttling === 'object' ) {
303+ this . contextThrottling . set ( command . contextId , {
304+ latency : Number ( command . throttling . latency ) || 0 ,
305+ downloadThroughput : Number ( command . throttling . downloadThroughput ) || 0 ,
306+ uploadThroughput : Number ( command . throttling . uploadThroughput ) || 0 ,
307+ } ) ;
308+ }
309+ return { success : true } ;
292310 case 'route' :
293311 await context . route ( command . url , async ( route ) => {
294312 const routeId = `route_${ ++ this . routeCounter } ` ;
295- this . routes . set ( routeId , route ) ;
313+ this . routes . set ( routeId , { route, contextId : command . contextId } ) ;
296314
297315 const req = route . request ( ) ;
298316 let postData = null ;
@@ -326,6 +344,62 @@ class PlaywrightServer {
326344 const page = await context . newPage ( command . options ) ;
327345 const pageId = `page_${ ++ this . pageCounter } ` ;
328346 this . pages . set ( pageId , page ) ;
347+ this . pageContexts . set ( pageId , command . contextId ) ;
348+
349+ // Forward important page events
350+ page . on ( 'console' , ( msg ) => {
351+ const params = {
352+ type : msg . type ( ) ,
353+ text : msg . text ( ) ,
354+ args : [ ] ,
355+ location : msg . location ? msg . location ( ) : { } ,
356+ } ;
357+ sendFramedResponse ( { objectId : pageId , event : 'console' , params } ) ;
358+ } ) ;
359+
360+ page . on ( 'dialog' , ( dialog ) => {
361+ const dialogId = `dialog_${ Date . now ( ) } _${ Math . floor ( Math . random ( ) * 1000 ) } ` ;
362+ this . dialogs . set ( dialogId , dialog ) ;
363+ const params = {
364+ dialogId,
365+ type : dialog . type ( ) ,
366+ message : dialog . message ( ) ,
367+ defaultValue : dialog . defaultValue ? dialog . defaultValue ( ) : null ,
368+ } ;
369+ sendFramedResponse ( { objectId : pageId , event : 'dialog' , params } ) ;
370+ } ) ;
371+
372+ page . on ( 'request' , ( req ) => {
373+ const params = {
374+ request : {
375+ url : req . url ( ) ,
376+ method : req . method ( ) ,
377+ headers : req . headers ( ) ,
378+ postData : ( ( ) => { try { return req . postData ( ) ; } catch { return null ; } } ) ( ) ,
379+ resourceType : req . resourceType ? req . resourceType ( ) : 'document' ,
380+ }
381+ } ;
382+ sendFramedResponse ( { objectId : pageId , event : 'request' , params } ) ;
383+ } ) ;
384+
385+ page . on ( 'response' , ( res ) => {
386+ const params = { response : this . serializeResponse ( res ) } ;
387+ sendFramedResponse ( { objectId : pageId , event : 'response' , params } ) ;
388+ } ) ;
389+
390+ page . on ( 'requestfailed' , ( req ) => {
391+ const params = {
392+ request : {
393+ url : req . url ( ) ,
394+ method : req . method ( ) ,
395+ headers : req . headers ( ) ,
396+ postData : ( ( ) => { try { return req . postData ( ) ; } catch { return null ; } } ) ( ) ,
397+ resourceType : req . resourceType ? req . resourceType ( ) : 'document' ,
398+ }
399+ } ;
400+ sendFramedResponse ( { objectId : pageId , event : 'requestfailed' , params } ) ;
401+ } ) ;
402+
329403 return { pageId : pageId } ;
330404 case 'cookies' :
331405 return { cookies : await context . cookies ( command . urls ) } ;
@@ -439,10 +513,22 @@ class PlaywrightServer {
439513 case 'addStyleTag' :
440514 await page . addStyleTag ( command . options ) ;
441515 return { success : true } ;
516+ case 'handleDialog' :
517+ const dialog = this . dialogs . get ( command . dialogId ) ;
518+ if ( dialog ) {
519+ if ( command . accept ) {
520+ await dialog . accept ( command . promptText ) ;
521+ } else {
522+ await dialog . dismiss ( ) ;
523+ }
524+ this . dialogs . delete ( command . dialogId ) ;
525+ }
526+ return { success : true } ;
442527 case 'route' :
443528 await page . route ( command . url , async ( route ) => {
444529 const routeId = `route_${ ++ this . routeCounter } ` ;
445- this . routes . set ( routeId , route ) ;
530+ const contextId = this . pageContexts . get ( command . pageId ) ;
531+ this . routes . set ( routeId , { route, contextId } ) ;
446532
447533 const req = route . request ( ) ;
448534 let postData = null ;
@@ -547,6 +633,9 @@ class PlaywrightServer {
547633 case 'screenshot' :
548634 const buffer = await locator . screenshot ( command . options ) ;
549635 return { binary : buffer . toString ( 'base64' ) } ;
636+ case 'setInputFiles' :
637+ await locator . setInputFiles ( command . files , command . options ) ;
638+ return ;
550639 case 'evaluate' :
551640 try {
552641 const count = await locator . count ( ) ;
@@ -596,8 +685,9 @@ class PlaywrightServer {
596685 }
597686
598687 async handleRouteCommand ( command , method ) {
599- const route = this . routes . get ( command . routeId ) ;
600- if ( ! route ) throw new Error ( `Route not found: ${ command . routeId } ` ) ;
688+ const info = this . routes . get ( command . routeId ) ;
689+ if ( ! info ) throw new Error ( `Route not found: ${ command . routeId } ` ) ;
690+ const route = info . route || info ;
601691
602692 switch ( method ) {
603693 case 'fulfill' :
@@ -610,6 +700,13 @@ class PlaywrightServer {
610700 break ;
611701 case 'continue' :
612702 debugLogger . info ( 'ROUTE CONTINUE' , { routeId : command . routeId } ) ;
703+ try {
704+ const ctxId = info . contextId ;
705+ const throttle = ctxId ? this . contextThrottling . get ( ctxId ) : null ;
706+ if ( throttle && throttle . latency && throttle . latency > 0 ) {
707+ await new Promise ( r => setTimeout ( r , throttle . latency ) ) ;
708+ }
709+ } catch { }
613710 await route . continue ( command . options || undefined ) ;
614711 break ;
615712 default :
@@ -618,6 +715,18 @@ class PlaywrightServer {
618715 this . routes . delete ( command . routeId ) ;
619716 }
620717
718+ async handleResponseCommand ( command , method ) {
719+ switch ( method ) {
720+ case 'body' :
721+ const res = this . responses . get ( command . responseId ) ;
722+ if ( ! res ) throw new Error ( `Response not found: ${ command . responseId } ` ) ;
723+ const buf = await res . body ( ) ;
724+ return { binary : Buffer . from ( buf ) . toString ( 'base64' ) } ;
725+ default :
726+ throw new Error ( `Unknown response action: ${ method } ` ) ;
727+ }
728+ }
729+
621730 async handleMouseCommand ( command , method ) {
622731 const page = this . pages . get ( command . pageId ) ;
623732 if ( ! page ) throw new Error ( `Page not found: ${ command . pageId } ` ) ;
0 commit comments