@@ -395,12 +395,9 @@ async def waitForNavigation(
395395 ) -> Optional [Response ]:
396396 return await self ._main_frame .waitForNavigation (** locals_to_params (locals ()))
397397
398- async def waitForRequest (
399- self ,
400- url : URLMatch = None ,
401- predicate : Callable [[Request ], bool ] = None ,
402- timeout : int = None ,
403- ) -> Request :
398+ def _get_request_predicate (
399+ self , url : Optional [URLMatch ], predicate : Optional [Callable [[Request ], bool ]]
400+ ) -> Callable [[Any ], bool ]:
404401 matcher = URLMatcher (url ) if url else None
405402
406403 def my_predicate (request : Request ) -> bool :
@@ -410,10 +407,34 @@ def my_predicate(request: Request) -> bool:
410407 return predicate (request )
411408 return True
412409
410+ return my_predicate
411+
412+ def _get_response_predicate (
413+ self , url : Optional [URLMatch ], predicate : Optional [Callable [[Response ], bool ]]
414+ ) -> Callable [[Any ], bool ]:
415+ matcher = URLMatcher (url ) if url else None
416+
417+ def my_predicate (response : Response ) -> bool :
418+ if matcher :
419+ return matcher .matches (response .url )
420+ if predicate :
421+ return predicate (response )
422+ return True
423+
424+ return my_predicate
425+
426+ async def waitForRequest (
427+ self ,
428+ url : URLMatch = None ,
429+ predicate : Callable [[Request ], bool ] = None ,
430+ timeout : int = None ,
431+ ) -> Request :
432+ predicate = self ._get_request_predicate (url , predicate )
433+
413434 return cast (
414435 Request ,
415436 await self .waitForEvent (
416- Page .Events .Request , predicate = my_predicate , timeout = timeout
437+ Page .Events .Request , predicate = predicate , timeout = timeout
417438 ),
418439 )
419440
@@ -423,19 +444,12 @@ async def waitForResponse(
423444 predicate : Callable [[Response ], bool ] = None ,
424445 timeout : int = None ,
425446 ) -> Response :
426- matcher = URLMatcher (url ) if url else None
427-
428- def my_predicate (response : Response ) -> bool :
429- if matcher :
430- return matcher .matches (response .url )
431- if predicate :
432- return predicate (response )
433- return True
447+ predicate = self ._get_response_predicate (url , predicate )
434448
435449 return cast (
436450 Response ,
437451 await self .waitForEvent (
438- Page .Events .Response , predicate = my_predicate , timeout = timeout
452+ Page .Events .Response , predicate = predicate , timeout = timeout
439453 ),
440454 )
441455
@@ -741,13 +755,21 @@ def expect_file_chooser(
741755 return EventContextManagerImpl (self , "filechooser" , predicate , timeout )
742756
743757 def expect_request (
744- self , predicate : Callable [[Request ], bool ] = None , timeout : int = None ,
758+ self ,
759+ url : URLMatch = None ,
760+ predicate : Callable [[Request ], bool ] = None ,
761+ timeout : int = None ,
745762 ) -> EventContextManagerImpl [Request ]:
763+ predicate = self ._get_request_predicate (url , predicate )
746764 return EventContextManagerImpl (self , "request" , predicate , timeout )
747765
748766 def expect_response (
749- self , predicate : Callable [[Response ], bool ] = None , timeout : int = None ,
767+ self ,
768+ url : URLMatch = None ,
769+ predicate : Callable [[Response ], bool ] = None ,
770+ timeout : int = None ,
750771 ) -> EventContextManagerImpl [Response ]:
772+ predicate = self ._get_response_predicate (url , predicate )
751773 return EventContextManagerImpl (self , "response" , predicate , timeout )
752774
753775 def expect_popup (
0 commit comments