tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

timeout.py (3079B)


      1 # This Source Code Form is subject to the terms of the Mozilla Public
      2 # License, v. 2.0. If a copy of the MPL was not distributed with this
      3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
      4 
      5 from . import errors
      6 
      7 DEFAULT_SCRIPT_TIMEOUT = 30
      8 DEFAULT_PAGE_LOAD_TIMEOUT = 300
      9 DEFAULT_IMPLICIT_WAIT_TIMEOUT = 0
     10 
     11 
     12 class Timeouts:
     13    """Manage timeout settings in the Marionette session.
     14 
     15    Usage::
     16 
     17        marionette = Marionette(...)
     18        marionette.start_session()
     19        marionette.timeout.page_load = 10
     20        marionette.timeout.page_load
     21        # => 10
     22 
     23    """
     24 
     25    def __init__(self, marionette):
     26        self._marionette = marionette
     27 
     28    def _set(self, name, sec):
     29        ms = sec * 1000
     30        self._marionette._send_message("WebDriver:SetTimeouts", {name: ms})
     31 
     32    def _get(self, name):
     33        ts = self._marionette._send_message("WebDriver:GetTimeouts")
     34        if name not in ts:
     35            raise KeyError()
     36        ms = ts[name]
     37        return ms / 1000.0
     38 
     39    @property
     40    def script(self):
     41        """Get the session's script timeout.  This specifies the time
     42        to wait for injected scripts to finished before interrupting
     43        them. It is by default 30 seconds.
     44 
     45        """
     46        return self._get("script")
     47 
     48    @script.setter
     49    def script(self, sec):
     50        """Set the session's script timeout.  This specifies the time
     51        to wait for injected scripts to finish before interrupting them.
     52 
     53        """
     54        self._set("script", sec)
     55 
     56    @property
     57    def page_load(self):
     58        """Get the session's page load timeout.  This specifies the time
     59        to wait for the page loading to complete.  It is by default 5
     60        minutes (or 300 seconds).
     61 
     62        """
     63        # remove fallback when Firefox 56 is stable
     64        try:
     65            return self._get("pageLoad")
     66        except KeyError:
     67            return self._get("page load")
     68 
     69    @page_load.setter
     70    def page_load(self, sec):
     71        """Set the session's page load timeout.  This specifies the time
     72        to wait for the page loading to complete.
     73 
     74        """
     75        # remove fallback when Firefox 56 is stable
     76        try:
     77            self._set("pageLoad", sec)
     78        except errors.InvalidArgumentException:
     79            return self._set("page load", sec)
     80 
     81    @property
     82    def implicit(self):
     83        """Get the session's implicit wait timeout.  This specifies the
     84        time to wait for the implicit element location strategy when
     85        retrieving elements.  It is by default disabled (0 seconds).
     86 
     87        """
     88        return self._get("implicit")
     89 
     90    @implicit.setter
     91    def implicit(self, sec):
     92        """Set the session's implicit wait timeout.  This specifies the
     93        time to wait for the implicit element location strategy when
     94        retrieving elements.
     95 
     96        """
     97        self._set("implicit", sec)
     98 
     99    def reset(self):
    100        """Resets timeouts to their default values."""
    101        self.script = DEFAULT_SCRIPT_TIMEOUT
    102        self.page_load = DEFAULT_PAGE_LOAD_TIMEOUT
    103        self.implicit = DEFAULT_IMPLICIT_WAIT_TIMEOUT