From 700d0ac5dec71f0b7b8b55e340c11d3f01f03955 Mon Sep 17 00:00:00 2001 From: sandeepsuryaprasad Date: Thu, 29 Jun 2023 15:49:42 +0530 Subject: [PATCH 1/5] [py] optimized Timeouts class by moving timeouts to descriptor class --- py/selenium/webdriver/common/timeouts.py | 109 ++++++++++++++--------- 1 file changed, 65 insertions(+), 44 deletions(-) diff --git a/py/selenium/webdriver/common/timeouts.py b/py/selenium/webdriver/common/timeouts.py index 82ce537e40cee..d6f01841703d2 100644 --- a/py/selenium/webdriver/common/timeouts.py +++ b/py/selenium/webdriver/common/timeouts.py @@ -37,6 +37,28 @@ 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): + if not isinstance(value, (int, float)): + raise TypeError("Timeouts can only be an int or float") + setattr(obj, self.name, int(float(value) * 1000)) + + class Timeouts: def __init__(self, implicit_wait: float = 0, page_load: float = 0, script: float = 0) -> None: """Create a new Timeout object. @@ -53,66 +75,65 @@ 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. + # Creating descriptor objects + implicit_wait = _TimeoutsDescriptor("_implicit_wait") + """Sets and Gets the value of the implicit_timeout: - This does not return the value on the remote end - """ - return self._implicit_wait / 1000 + This does not set the value on the remote end. - @implicit_wait.setter - def implicit_wait(self, _implicit_wait: float) -> None: - """Sets the value for the implicit wait. + Usage + ----- + - Get + - `self.implicit_timeout` + - Set + - `self.implicit_timeout` = `value` - This does not set the value on the remote end - """ - self._implicit_wait = self._convert(_implicit_wait) + Parameters + ---------- + `value`: `float` + """ - @property - def page_load(self) -> float: - """Return the value for the page load wait. + page_load = _TimeoutsDescriptor("_page_load") + """Sets and Gets the value of page load wait: - This does not return the value on the remote end - """ - return self._page_load / 1000 + This does not set the value on the remote end. - @page_load.setter - def page_load(self, _page_load: float) -> None: - """Sets the value for the page load wait. + Usage + ----- + - Get + - `self.page_load` + - Set + - `self.page_load` = `value` - This does not set the value on the remote end - """ - self._page_load = self._convert(_page_load) + Parameters + ---------- + `value`: `float` + """ - @property - def script(self) -> float: - """Return the value for the script wait. + script = _TimeoutsDescriptor("_script") + """Sets and Gets the value of 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. - This does not set the value on the remote end - """ - self._script = self._convert(_script) + Usage + ------ + - Get + - `self.script` + - Set + - `self.script` = `value` - def _convert(self, timeout: float) -> int: - if isinstance(timeout, (int, float)): - return int(float(timeout) * 1000) - raise TypeError("Timeouts can only be an int or a float") + Parameters + ----------- + `value`: `float` + """ def _to_json(self) -> JSONTimeouts: timeouts: JSONTimeouts = {} if self._implicit_wait: - timeouts["implicit"] = self._implicit_wait + timeouts["implicit"] = self.implicit_wait if self._page_load: - timeouts["pageLoad"] = self._page_load + timeouts["pageLoad"] = self.page_load if self._script: - timeouts["script"] = self._script + timeouts["script"] = self.script return timeouts From 04154400de5358eb22e768e808984ce88d3bef20 Mon Sep 17 00:00:00 2001 From: sandeepsuryaprasad Date: Tue, 4 Jul 2023 17:16:12 +0530 Subject: [PATCH 2/5] fixed lynting error --- py/selenium/webdriver/ie/options.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/selenium/webdriver/ie/options.py b/py/selenium/webdriver/ie/options.py index e64f41127b2cb..8c7b355c7b5a1 100644 --- a/py/selenium/webdriver/ie/options.py +++ b/py/selenium/webdriver/ie/options.py @@ -387,4 +387,4 @@ def to_capabilities(self) -> dict: @property def default_capabilities(self) -> dict: - return DesiredCapabilities.INTERNETEXPLORER.copy() \ No newline at end of file + DesiredCapabilities.INTERNETEXPLORER.copy() From a7040530ff067d94ef698e179504948267568baf Mon Sep 17 00:00:00 2001 From: sandeepsuryaprasad Date: Tue, 4 Jul 2023 21:13:11 +0530 Subject: [PATCH 3/5] added return statement in default_capabilities in ie/options.py --- py/selenium/webdriver/ie/options.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/selenium/webdriver/ie/options.py b/py/selenium/webdriver/ie/options.py index 8c7b355c7b5a1..94b56f40a2f89 100644 --- a/py/selenium/webdriver/ie/options.py +++ b/py/selenium/webdriver/ie/options.py @@ -387,4 +387,4 @@ def to_capabilities(self) -> dict: @property def default_capabilities(self) -> dict: - DesiredCapabilities.INTERNETEXPLORER.copy() + return DesiredCapabilities.INTERNETEXPLORER.copy() From 861a7891a1a714eca4f6dfc1c5b4f8b230af60bd Mon Sep 17 00:00:00 2001 From: sandeepsuryaprasad Date: Thu, 6 Jul 2023 07:29:23 +0530 Subject: [PATCH 4/5] [py] fixed bug that was causing UI tests to fail --- py/selenium/webdriver/common/timeouts.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/py/selenium/webdriver/common/timeouts.py b/py/selenium/webdriver/common/timeouts.py index d6f01841703d2..3077c25b9a793 100644 --- a/py/selenium/webdriver/common/timeouts.py +++ b/py/selenium/webdriver/common/timeouts.py @@ -53,10 +53,9 @@ def __init__(self, name): def __get__(self, obj, cls) -> float: return getattr(obj, self.name) / 1000 - def __set__(self, obj, value): - if not isinstance(value, (int, float)): - raise TypeError("Timeouts can only be an int or float") - setattr(obj, self.name, int(float(value) * 1000)) + def __set__(self, obj, value) -> None: + converted_value = getattr(obj, "_convert")(value) + setattr(obj, self.name, converted_value) class Timeouts: @@ -127,13 +126,18 @@ def __init__(self, implicit_wait: float = 0, page_load: float = 0, script: float `value`: `float` """ + def _convert(self, timeout: float) -> int: + if isinstance(timeout, (int, float)): + return int(float(timeout) * 1000) + raise TypeError("Timeouts can only be an int or float") + def _to_json(self) -> JSONTimeouts: timeouts: JSONTimeouts = {} if self._implicit_wait: - timeouts["implicit"] = self.implicit_wait + timeouts["implicit"] = self._implicit_wait if self._page_load: - timeouts["pageLoad"] = self.page_load + timeouts["pageLoad"] = self._page_load if self._script: - timeouts["script"] = self.script + timeouts["script"] = self._script return timeouts From a6be8cf8977f21eb0cf69ae1db6dbca7a5d5e352 Mon Sep 17 00:00:00 2001 From: sandeepsuryaprasad Date: Thu, 6 Jul 2023 07:33:51 +0530 Subject: [PATCH 5/5] [py] fixed a bug that was causing UI tests to fail --- py/selenium/webdriver/common/timeouts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/selenium/webdriver/common/timeouts.py b/py/selenium/webdriver/common/timeouts.py index 3077c25b9a793..0f7f78e2317fe 100644 --- a/py/selenium/webdriver/common/timeouts.py +++ b/py/selenium/webdriver/common/timeouts.py @@ -129,7 +129,7 @@ def __init__(self, implicit_wait: float = 0, page_load: float = 0, script: float def _convert(self, timeout: float) -> int: if isinstance(timeout, (int, float)): return int(float(timeout) * 1000) - raise TypeError("Timeouts can only be an int or float") + raise TypeError("Timeouts can only be an int or a float") def _to_json(self) -> JSONTimeouts: timeouts: JSONTimeouts = {}