def frame(self, name: str = None, url: URLMatch = None) -> Optional[Frame]: matcher = URLMatcher(url) if url else None for frame in self._frames: if name and frame.name == name: return frame if url and matcher and matcher.matches(frame.url): return frame return None
async def waitForNavigation( self, timeout: int = None, waitUntil: DocumentLoadState = "load", url: URLMatch = None, ) -> Optional[Response]: wait_helper = self._setup_navigation_wait_helper(timeout) matcher = URLMatcher(url) if url else None event = await wait_helper.wait_for_event( self._event_emitter, "navigated", lambda event: not matcher or matcher.matches(event["url"]), ) if "newDocument" in event and "request" in event["newDocument"]: request = from_channel(event["newDocument"]["request"]) return await request.response() if "error" in event: raise Error(event["error"]) return None
async def waitForResponse( self, url: URLMatch = None, predicate: Callable[[Response], bool] = None, timeout: int = None, ) -> Optional[Response]: matcher = URLMatcher(url) if url else None def my_predicate(response: Response) -> bool: if matcher: return matcher.matches(response.url) if predicate: return predicate(response) return True return cast( Optional[Response], await self.waitForEvent( Page.Events.Response, predicate=my_predicate, timeout=timeout ), )
async def waitForRequest( self, url: URLMatch = None, predicate: Callable[[Request], bool] = None, timeout: int = None, ) -> Request: matcher = URLMatcher(url) if url else None def my_predicate(request: Request) -> bool: if matcher: return matcher.matches(request.url) if predicate: return predicate(request) return True return cast( Request, await self.waitForEvent(Page.Events.Request, predicate=my_predicate, timeout=timeout), )
async def waitForNavigation( self, url: URLMatch = None, waitUntil: DocumentLoadState = None, timeout: int = None, ) -> Optional[Response]: if not waitUntil: waitUntil = "load" if timeout is None: timeout = self._page._timeout_settings.navigation_timeout() deadline = monotonic_time() + timeout wait_helper = self._setup_navigation_wait_helper(timeout) matcher = URLMatcher(url) if url else None def predicate(event: Any) -> bool: # Any failed navigation results in a rejection. if event.get("error"): return True return not matcher or matcher.matches(event["url"]) event = await wait_helper.wait_for_event( self._event_emitter, "navigated", predicate=predicate, ) if "error" in event: raise Error(event["error"]) if waitUntil not in self._load_states: timeout = deadline - monotonic_time() if timeout > 0: await self.waitForLoadState(state=waitUntil, timeout=timeout) if "newDocument" in event and "request" in event["newDocument"]: request = from_channel(event["newDocument"]["request"]) return await request.response() return None
async def route(self, url: URLMatch, handler: RouteHandler) -> None: self._routes.append(RouteHandlerEntry(URLMatcher(url), handler)) if len(self._routes) == 1: await self._channel.send( "setNetworkInterceptionEnabled", dict(enabled=True) )