Пример #1
0
 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
Пример #2
0
    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),
        )
Пример #3
0
    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))