async def route(self, url: URLMatch, handler: RouteHandlerCallback, times: int = None) -> None: self._routes.insert( 0, RouteHandler( URLMatcher(self._browser_context._options.get("baseURL"), url), handler, times, ), ) if len(self._routes) == 1: await self._channel.send("setNetworkInterceptionEnabled", dict(enabled=True))
def expect_navigation( self, url: URLMatch = None, wait_until: DocumentLoadState = None, timeout: float = None, ) -> EventContextManagerImpl[Response]: if not wait_until: wait_until = "load" if timeout is None: timeout = self._page._timeout_settings.navigation_timeout() deadline = monotonic_time() + timeout wait_helper = self._setup_navigation_wait_helper( "expect_navigation", timeout) to_url = f' to "{url}"' if url else "" wait_helper.log(f"waiting for navigation{to_url} until '{wait_until}'") matcher = (URLMatcher( self._page._browser_context._options.get("baseURL"), url) if url else None) def predicate(event: Any) -> bool: # Any failed navigation results in a rejection. if event.get("error"): return True wait_helper.log(f' navigated to "{event["url"]}"') return not matcher or matcher.matches(event["url"]) wait_helper.wait_for_event( self._event_emitter, "navigated", predicate=predicate, ) async def continuation() -> Optional[Response]: event = await wait_helper.result() if "error" in event: raise Error(event["error"]) if wait_until not in self._load_states: t = deadline - monotonic_time() if t > 0: await self.wait_for_load_state(state=wait_until, timeout=t) if "newDocument" in event and "request" in event["newDocument"]: request = from_channel(event["newDocument"]["request"]) return await request.response() return None return EventContextManagerImpl(asyncio.create_task(continuation()))
def expect_response( self, url_or_predicate: URLMatchResponse, timeout: float = None, ) -> EventContextManagerImpl[Response]: matcher = None if callable(url_or_predicate) else URLMatcher(url_or_predicate) predicate = url_or_predicate if callable(url_or_predicate) else None def my_predicate(response: Response) -> bool: if matcher: return matcher.matches(response.url) if predicate: return predicate(response) return True return self.expect_event( Page.Events.Response, predicate=my_predicate, timeout=timeout )
async def waitForResponse( self, url: URLMatch = None, predicate: Callable[[Response], bool] = None, timeout: int = None, ) -> 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( Response, await self.waitForEvent(Page.Events.Response, predicate=my_predicate, timeout=timeout), )
async def wait_for_response( self, urlOrPredicate: URLMatchResponse, timeout: float = None, ) -> Response: matcher = None if callable(urlOrPredicate) else URLMatcher(urlOrPredicate) predicate = urlOrPredicate if callable(urlOrPredicate) else None def my_predicate(response: Response) -> bool: if matcher: return matcher.matches(response.url) if predicate: return predicate(response) return True return cast( Response, await self.wait_for_event( Page.Events.Response, predicate=my_predicate, timeout=timeout ), )
async def waitForRequest( self, urlOrPredicate: URLMatchRequest, timeout: float = None, ) -> Request: matcher = None if callable(urlOrPredicate) else URLMatcher( urlOrPredicate) predicate = urlOrPredicate if callable(urlOrPredicate) else None def my_predicate(request: Request) -> bool: if matcher: return matcher.matches(request.url) if predicate: return urlOrPredicate(request) return True return cast( Request, await self.waitForEvent(Page.Events.Request, predicate=my_predicate, timeout=timeout), )
async def wait_for_navigation( self, url: URLMatch = None, waitUntil: DocumentLoadState = None, timeout: float = 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: t = deadline - monotonic_time() if t > 0: await self.wait_for_load_state(state=waitUntil, timeout=t) if "newDocument" in event and "request" in event["newDocument"]: request = from_channel(event["newDocument"]["request"]) return await request.response() return None
def expect_response( self, url_or_predicate: URLMatchResponse, timeout: float = None, ) -> EventContextManagerImpl[Response]: matcher = (None if callable(url_or_predicate) else URLMatcher( self._browser_context._options.get("baseURL"), url_or_predicate)) predicate = url_or_predicate if callable(url_or_predicate) else None def my_predicate(response: Response) -> bool: if matcher: return matcher.matches(response.url) if predicate: return predicate(response) return True trimmed_url = trim_url(url_or_predicate) log_line = f"waiting for response {trimmed_url}" if trimmed_url else None return self._expect_event( Page.Events.Response, predicate=my_predicate, timeout=timeout, log_line=log_line, )
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))