Exemplo n.º 1
0
    def insist(self, fn, ttl=None):
        """Wait until a particular condition is met and then assert

        This is very similar to :meth:`until` with the only difference being
        that it will perform an ``assert`` instead of raising a TimeOutError.

        Yes, yes. It would have been nice to call this method `assert`, but
        that unfortunately is a reserved keyword, so you'll have to make due.

        :param fn: A function that describes the condition that must be
                   met before we can return the elements. E.g. if the page
                   is loading and we need to get all links (and we know that
                   there should be three, but the third one takes a while
                   to load), we could do something like::

                    links = elements.
                        find('a').
                        insist(lambda e: len(e) == 3)

                    The function takes one parameter, an Elements object and
                    should return True or False

        :param ttl: The minimum number of seconds to keep retrying
        :returns: ``self``
        """
        ttl = ttl if ttl is not None else self.ttl
        with ignored(TimeOutError):
            ConditionElementsWaiter(self).wait(fn, ttl=ttl)
        assert fn(self)
        return self
Exemplo n.º 2
0
    def insist(self, fn, ttl=None):
        """Wait until a particular condition is met and then assert

        This is very similar to :meth:`until` with the only difference being
        that it will perform an ``assert`` instead of raising a TimeOutError.

        Yes, yes. It would have been nice to call this method `assert`, but
        that unfortunately is a reserved keyword, so you'll have to make due.

        :param fn: A function that describes the condition that must be
                   met before we can return the elements. E.g. if the page
                   is loading and we need to get all links (and we know that
                   there should be three, but the third one takes a while
                   to load), we could do something like::

                    links = elements.
                        find('a').
                        insist(lambda e: len(e) == 3)

                    The function takes one parameter, an Elements object and
                    should return True or False

        :param ttl: The minimum number of seconds to keep retrying
        :returns: ``self``
        """
        ttl = ttl if ttl is not None else self.ttl
        with ignored(TimeOutError):
            ConditionElementsWaiter(self).wait(fn, ttl=ttl)
        assert fn(self)
        return self
Exemplo n.º 3
0
    def wait(self, fn, n=0, ttl=None):
        """Wait until a particular condition is met

        :param fn: The function that corresponds to the condition that must be
                   met. Once this function returns ``True``, it will return.
                   If ``True`` is not returned in the provided amount of time,
                   a :class:`TimeOutError`
        :param n: The number of times to retry. This will override what is
                  passed to the constructor
        :param ttl: The number of seconds to wait. This will override what is
                    passed to the constructor
        :returns: The :class:`Elements` object that was passed to the
                  constructor
        """
        n, ttl = self._check_args(n, ttl)
        etime = time.time() + ttl
        pause = self.pause
        while time.time() < etime or n > 0:
            n -= 1
            if not fn(self.elements):
                time.sleep(pause)
                pause = min(pause + pause, pause * 4)
                self.elements.update()
            else:
                break
        else:
            reason = "Unknown"
            with ignored(Exception):
                reason = inspect.getsource(fn)
                reason = reason.strip()
            raise TimeOutError(reason)
        return self.elements
Exemplo n.º 4
0
    def wait(self, fn, n=0, ttl=None):
        """Wait until a particular condition is met

        :param fn: The function that corresponds to the condition that must be
                   met. Once this function returns ``True``, it will return.
                   If ``True`` is not returned in the provided amount of time,
                   a :class:`TimeOutError`
        :param n: The number of times to retry. This will override what is
                  passed to the constructor
        :param ttl: The number of seconds to wait. This will override what is
                    passed to the constructor
        :returns: The :class:`Elements` object that was passed to the
                  constructor
        """
        n, ttl = self._check_args(n, ttl)
        etime = time.time() + ttl
        pause = self.pause
        while time.time() < etime or n > 0:
            n -= 1
            if not fn(self.elements):
                time.sleep(pause)
                pause = min(pause + pause, pause * 4)
                self.elements.update()
            else:
                break
        else:
            reason = "Unknown"
            with ignored(Exception):
                reason = inspect.getsource(fn)
                reason = reason.strip()
            raise TimeOutError(reason)
        return self.elements
Exemplo n.º 5
0
    def get_window_size(self):
        """Get the size of the browser window

        :returns: A tuple of the form ``(width, height)`` where the units are
                  pixels
        """
        with ignored(Exception):
            dim = self.browser.get_window_size()
            return (dim['width'], dim['height'])
Exemplo n.º 6
0
    def get_window_size(self):
        """Get the size of the browser window

        :returns: A tuple of the form ``(width, height)`` where the units are
                  pixels
        """
        with ignored(Exception):
            dim = self.browser.get_window_size()
            return (dim['width'], dim['height'])
Exemplo n.º 7
0
    def set_window_size(self, width, height, sleep=DEFAULT_SLEEP_TIME):
        """Set the size of the browser window

        :param width: Browser width in pixels
        :param height: Browser height in pixels
        :param sleep: The number of seconds to sleep after the command to make
                      sure the command has been run
        :returns: ``self``
        """
        with ignored(Exception):
            self.browser.set_window_size(width, height)
            if sleep:
                time.sleep(sleep)
        return self
Exemplo n.º 8
0
    def set_window_size(self, width, height, sleep=DEFAULT_SLEEP_TIME):
        """Set the size of the browser window

        :param width: Browser width in pixels
        :param height: Browser height in pixels
        :param sleep: The number of seconds to sleep after the command to make
                      sure the command has been run
        :returns: ``self``
        """
        with ignored(Exception):
            self.browser.set_window_size(width, height)
            if sleep:
                time.sleep(sleep)
        return self