Skip to content

Commit 4468622

Browse files
[py] optimized Timeouts class by moving timeouts to descriptor class (#12287)
* [py] optimized Timeouts class by moving timeouts to descriptor class * fixed lynting error * added return statement in default_capabilities in ie/options.py * [py] fixed bug that was causing UI tests to fail * [py] fixed a bug that was causing UI tests to fail --------- Co-authored-by: Diego Molina <[email protected]>
1 parent 669fbb3 commit 4468622

1 file changed

Lines changed: 72 additions & 47 deletions

File tree

py/selenium/webdriver/common/timeouts.py

Lines changed: 72 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,27 @@ class JSONTimeouts(TypedDict, total=False):
3737
JSONTimeouts = Dict[str, int]
3838

3939

40+
class _TimeoutsDescriptor:
41+
"""TimeoutsDescriptor which gets and sets value of below attributes:
42+
43+
_implicit _timeout
44+
_page_load
45+
_script
46+
47+
This does not set the value on the remote end
48+
"""
49+
50+
def __init__(self, name):
51+
self.name = name
52+
53+
def __get__(self, obj, cls) -> float:
54+
return getattr(obj, self.name) / 1000
55+
56+
def __set__(self, obj, value) -> None:
57+
converted_value = getattr(obj, "_convert")(value)
58+
setattr(obj, self.name, converted_value)
59+
60+
4061
class Timeouts:
4162
def __init__(self, implicit_wait: float = 0, page_load: float = 0, script: float = 0) -> None:
4263
"""Create a new Timeout object.
@@ -53,53 +74,57 @@ def __init__(self, implicit_wait: float = 0, page_load: float = 0, script: float
5374
self.page_load = page_load
5475
self.script = script
5576

56-
@property
57-
def implicit_wait(self) -> float:
58-
"""Return the value for the implicit wait.
59-
60-
This does not return the value on the remote end
61-
"""
62-
return self._implicit_wait / 1000
63-
64-
@implicit_wait.setter
65-
def implicit_wait(self, _implicit_wait: float) -> None:
66-
"""Sets the value for the implicit wait.
67-
68-
This does not set the value on the remote end
69-
"""
70-
self._implicit_wait = self._convert(_implicit_wait)
71-
72-
@property
73-
def page_load(self) -> float:
74-
"""Return the value for the page load wait.
75-
76-
This does not return the value on the remote end
77-
"""
78-
return self._page_load / 1000
79-
80-
@page_load.setter
81-
def page_load(self, _page_load: float) -> None:
82-
"""Sets the value for the page load wait.
83-
84-
This does not set the value on the remote end
85-
"""
86-
self._page_load = self._convert(_page_load)
87-
88-
@property
89-
def script(self) -> float:
90-
"""Return the value for the script wait.
91-
92-
This does not return the value on the remote end
93-
"""
94-
return self._script / 1000
95-
96-
@script.setter
97-
def script(self, _script: float) -> None:
98-
"""Sets the value for the script wait.
99-
100-
This does not set the value on the remote end
101-
"""
102-
self._script = self._convert(_script)
77+
# Creating descriptor objects
78+
implicit_wait = _TimeoutsDescriptor("_implicit_wait")
79+
"""Sets and Gets the value of the implicit_timeout:
80+
81+
This does not set the value on the remote end.
82+
83+
Usage
84+
-----
85+
- Get
86+
- `self.implicit_timeout`
87+
- Set
88+
- `self.implicit_timeout` = `value`
89+
90+
Parameters
91+
----------
92+
`value`: `float`
93+
"""
94+
95+
page_load = _TimeoutsDescriptor("_page_load")
96+
"""Sets and Gets the value of page load wait:
97+
98+
This does not set the value on the remote end.
99+
100+
Usage
101+
-----
102+
- Get
103+
- `self.page_load`
104+
- Set
105+
- `self.page_load` = `value`
106+
107+
Parameters
108+
----------
109+
`value`: `float`
110+
"""
111+
112+
script = _TimeoutsDescriptor("_script")
113+
"""Sets and Gets the value of script wait:
114+
115+
This does not set the value on the remote end.
116+
117+
Usage
118+
------
119+
- Get
120+
- `self.script`
121+
- Set
122+
- `self.script` = `value`
123+
124+
Parameters
125+
-----------
126+
`value`: `float`
127+
"""
103128

104129
def _convert(self, timeout: float) -> int:
105130
if isinstance(timeout, (int, float)):

0 commit comments

Comments
 (0)