Example #1
0
    def __init__(self,
                 frame: Frame,
                 expr: str,
                 timeout: float,
                 *args: Any,
                 interval: float = 0) -> None:
        """Make new wait task.

        :arg float timeout: msec to wait for task [default 30_000 [msec]].
        :arg float interval: msec to poll for task [default timeout / 1000].
        """
        super().__init__()
        self.__frame: Frame = frame
        self.expr = expr
        self.args = args
        self.__timeout = timeout / 1000  # sec
        self.__interval = interval / 1000 or self.__timeout / 100  # sec
        self.__runCount: int = 0
        self.__terminated = False
        self.__done = False
        frame._waitTasks.add(self)
        # Since page navigation requires us to re-install the pageScript,
        # we should track timeout on our end.
        self.__loop = asyncio.get_event_loop()
        self.__timeoutTimer = self.__loop.call_later(
            self.__timeout, lambda: self.terminate(
                TimeoutError(f'waiting failed: timeout {timeout}ms exceeded')))
        asyncio.ensure_future(self.rerun(True))
 async def timer(timeout: Union[int, float]) -> None:
     await asyncio.sleep(timeout / 1000)
     self._timeoutError = True
     self.terminate(
         TimeoutError(
             f'Waiting for {title} failed: timeout {timeout}ms exceeds.'
         ))
Example #3
0
 async def timer(timeout: Union[(int, float)]) -> None:
     (await asyncio.sleep((timeout / 1000)))
     self._timeoutError = True
     self.terminate(
         TimeoutError(''.join([
             'Waiting for ', '{}'.format(title), ' failed: timeout ',
             '{}'.format(timeout), 'ms exceeds.'
         ])))
Example #4
0
 async def goto(self, url):
     """Navigate to address"""
     jquery_js = await load_file(self.jquery_data)
     await self.page.evaluateOnNewDocument("() => {\n%s}" % jquery_js
                                           )  # Inject JQuery
     await self.page.setExtraHTTPHeaders({'Accept-Language': self.language}
                                         )  # Forced set Language
     await fuckcaptcha.bypass_detections(
         self.page)  # bypass reCAPTCHA detection in pyppeteer
     retry = 3  # Go to Page and Retry 3 times
     while True:
         try:
             await self.loop.create_task(
                 self.page.goto(
                     url,
                     timeout=self.page_load_timeout * 1000,
                     waitUntil=["networkidle0", "domcontentloaded"]))
             break
         except asyncio.TimeoutError as ex:
             traceback.print_exc(file=sys.stdout)
             self.log('Error timeout: ' + str(ex) + ' retry ' + str(retry))
             if retry > 0:
                 retry -= 1
             else:
                 raise TimeoutError("Page loading timed-out")
         except PyppeteerError as ex:
             traceback.print_exc(file=sys.stdout)
             self.log(f"Pyppeteer error: {ex}")
             if retry > 0:
                 retry -= 1
             else:
                 raise ex
         except Exception as ex:
             traceback.print_exc(file=sys.stdout)
             self.log('Error unexpected: ' + str(ex) + ' retry ' +
                      str(retry))
             if retry > 0:
                 retry -= 1
             else:
                 raise PageError(f"Page raised an error: `{ex}`")
Example #5
0
    async def waitForTarget(self, predicate, options=None, **kwargs):
        options = merge_dict(options, kwargs)
        timeout = options.get("timeout", 30000)
        with contextlib.suppress(StopIteration):
            return next(filter(lambda u: u == predicate, self.targets()))
        resolve = self._connection._loop.create_future()

        def check(target):
            if predicate(target):
                resolve.set_result(target)

        self.on(Browser.Events.TargetCreated, check)
        self.on(Browser.Events.TargetChanged, check)
        try:
            if timeout:
                return await asyncio.wait_for(resolve, timeout=timeout)
            return await resolve
        except asyncio.TimeoutError:
            raise TimeoutError(
                "waiting for target target: "
                f"timeout {timeout * 1000}ms exceeded") from None
        finally:
            self.remove_listener(Browser.Events.TargetCreated, check)
            self.remove_listener(Browser.Events.TargetChanged, check)
 async def _timeout_func() -> None:
     await asyncio.sleep(self._timeout / 1000)
     self._maximumTimer.set_exception(TimeoutError(errorMessage))
Example #7
0
 async def timeoutTimer() -> None:
     await asyncio.sleep(timeout / 1000)
     rejectCallback(
         TimeoutError('Timeout exceeded while waiting for event'))