Skip to content

Commit

Permalink
[py] optimized Timeouts class by moving timeouts to descriptor class (#…
Browse files Browse the repository at this point in the history
…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 <diemol@users.noreply.github.com>
  • Loading branch information
sandeepsuryaprasad and diemol committed Jul 6, 2023
1 parent 669fbb3 commit 4468622
Showing 1 changed file with 72 additions and 47 deletions.
119 changes: 72 additions & 47 deletions py/selenium/webdriver/common/timeouts.py
Expand Up @@ -37,6 +37,27 @@ class JSONTimeouts(TypedDict, total=False):
JSONTimeouts = Dict[str, int]


class _TimeoutsDescriptor:
"""TimeoutsDescriptor which gets and sets value of below attributes:
_implicit _timeout
_page_load
_script
This does not set the value on the remote end
"""

def __init__(self, name):
self.name = name

def __get__(self, obj, cls) -> float:
return getattr(obj, self.name) / 1000

def __set__(self, obj, value) -> None:
converted_value = getattr(obj, "_convert")(value)
setattr(obj, self.name, converted_value)


class Timeouts:
def __init__(self, implicit_wait: float = 0, page_load: float = 0, script: float = 0) -> None:
"""Create a new Timeout object.
Expand All @@ -53,53 +74,57 @@ def __init__(self, implicit_wait: float = 0, page_load: float = 0, script: float
self.page_load = page_load
self.script = script

@property
def implicit_wait(self) -> float:
"""Return the value for the implicit wait.
This does not return the value on the remote end
"""
return self._implicit_wait / 1000

@implicit_wait.setter
def implicit_wait(self, _implicit_wait: float) -> None:
"""Sets the value for the implicit wait.
This does not set the value on the remote end
"""
self._implicit_wait = self._convert(_implicit_wait)

@property
def page_load(self) -> float:
"""Return the value for the page load wait.
This does not return the value on the remote end
"""
return self._page_load / 1000

@page_load.setter
def page_load(self, _page_load: float) -> None:
"""Sets the value for the page load wait.
This does not set the value on the remote end
"""
self._page_load = self._convert(_page_load)

@property
def script(self) -> float:
"""Return the value for the script wait.
This does not return the value on the remote end
"""
return self._script / 1000

@script.setter
def script(self, _script: float) -> None:
"""Sets the value for the script wait.
This does not set the value on the remote end
"""
self._script = self._convert(_script)
# Creating descriptor objects
implicit_wait = _TimeoutsDescriptor("_implicit_wait")
"""Sets and Gets the value of the implicit_timeout:
This does not set the value on the remote end.
Usage
-----
- Get
- `self.implicit_timeout`
- Set
- `self.implicit_timeout` = `value`
Parameters
----------
`value`: `float`
"""

page_load = _TimeoutsDescriptor("_page_load")
"""Sets and Gets the value of page load wait:
This does not set the value on the remote end.
Usage
-----
- Get
- `self.page_load`
- Set
- `self.page_load` = `value`
Parameters
----------
`value`: `float`
"""

script = _TimeoutsDescriptor("_script")
"""Sets and Gets the value of script wait:
This does not set the value on the remote end.
Usage
------
- Get
- `self.script`
- Set
- `self.script` = `value`
Parameters
-----------
`value`: `float`
"""

def _convert(self, timeout: float) -> int:
if isinstance(timeout, (int, float)):
Expand Down

0 comments on commit 4468622

Please sign in to comment.