summaryrefslogtreecommitdiffstats
path: root/testing/marionette/client/marionette_driver/timeout.py
blob: 99a9746082fdebc887646f734ebbcd6a26ec0208 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from __future__ import absolute_import

from . import errors


DEFAULT_SCRIPT_TIMEOUT = 30
DEFAULT_PAGE_LOAD_TIMEOUT = 300
DEFAULT_IMPLICIT_WAIT_TIMEOUT = 0


class Timeouts(object):
    """Manage timeout settings in the Marionette session.

    Usage::

        marionette = Marionette(...)
        marionette.start_session()
        marionette.timeout.page_load = 10
        marionette.timeout.page_load
        # => 10

    """

    def __init__(self, marionette):
        self._marionette = marionette

    def _set(self, name, sec):
        ms = sec * 1000
        self._marionette._send_message("WebDriver:SetTimeouts", {name: ms})

    def _get(self, name):
        ts = self._marionette._send_message("WebDriver:GetTimeouts")
        if name not in ts:
            raise KeyError()
        ms = ts[name]
        return ms / 1000.0

    @property
    def script(self):
        """Get the session's script timeout.  This specifies the time
        to wait for injected scripts to finished before interrupting
        them. It is by default 30 seconds.

        """
        return self._get("script")

    @script.setter
    def script(self, sec):
        """Set the session's script timeout.  This specifies the time
        to wait for injected scripts to finish before interrupting them.

        """
        self._set("script", sec)

    @property
    def page_load(self):
        """Get the session's page load timeout.  This specifies the time
        to wait for the page loading to complete.  It is by default 5
        minutes (or 300 seconds).

        """
        # remove fallback when Firefox 56 is stable
        try:
            return self._get("pageLoad")
        except KeyError:
            return self._get("page load")

    @page_load.setter
    def page_load(self, sec):
        """Set the session's page load timeout.  This specifies the time
        to wait for the page loading to complete.

        """
        # remove fallback when Firefox 56 is stable
        try:
            self._set("pageLoad", sec)
        except errors.InvalidArgumentException:
            return self._set("page load", sec)

    @property
    def implicit(self):
        """Get the session's implicit wait timeout.  This specifies the
        time to wait for the implicit element location strategy when
        retrieving elements.  It is by default disabled (0 seconds).

        """
        return self._get("implicit")

    @implicit.setter
    def implicit(self, sec):
        """Set the session's implicit wait timeout.  This specifies the
        time to wait for the implicit element location strategy when
        retrieving elements.

        """
        self._set("implicit", sec)

    def reset(self):
        """Resets timeouts to their default values."""
        self.script = DEFAULT_SCRIPT_TIMEOUT
        self.page_load = DEFAULT_PAGE_LOAD_TIMEOUT
        self.implicit = DEFAULT_IMPLICIT_WAIT_TIMEOUT