From b4eacd3dd67d5504081393f1138de3dddab9a4c7 Mon Sep 17 00:00:00 2001 From: sandeepsuryaprasad Date: Sun, 9 Jul 2023 18:41:52 +0530 Subject: [PATCH 1/4] [py] added type checks to setter methods in different browser options --- py/selenium/webdriver/chromium/options.py | 8 ++++++++ py/selenium/webdriver/firefox/options.py | 4 ++++ py/selenium/webdriver/safari/options.py | 8 ++++++++ py/selenium/webdriver/wpewebkit/options.py | 4 +++- 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/py/selenium/webdriver/chromium/options.py b/py/selenium/webdriver/chromium/options.py index 11cd6e65ee3bc..d9a00d852d5a9 100644 --- a/py/selenium/webdriver/chromium/options.py +++ b/py/selenium/webdriver/chromium/options.py @@ -51,6 +51,8 @@ def binary_location(self, value: str) -> None: :Args: - value: path to the Chromium binary """ + if not isinstance(value, str): + raise TypeError("Binary Location must be a string") self._binary_location = value @property @@ -69,6 +71,8 @@ def debugger_address(self, value: str) -> None: :Args: - value: address of remote devtools instance if any (hostname[:port]) """ + if not isinstance(value, str): + raise TypeError("Debugger Address must be a string") self._debugger_address = value @property @@ -162,6 +166,10 @@ def headless(self, value: bool) -> None: stacklevel=2, ) args = {"--headless"} + + if not isinstance(value, bool): + raise TypeError("value must be a boolean") + if value: self._arguments.extend(args) else: diff --git a/py/selenium/webdriver/firefox/options.py b/py/selenium/webdriver/firefox/options.py index ea5f688c18974..82261186c9c67 100644 --- a/py/selenium/webdriver/firefox/options.py +++ b/py/selenium/webdriver/firefox/options.py @@ -67,6 +67,8 @@ def binary_location(self) -> str: @binary_location.setter # noqa def binary_location(self, value: str) -> None: """Sets the location of the browser binary by string.""" + if not isinstance(value, str): + raise TypeError("Binary Location must be a string") self.binary = value @property @@ -122,6 +124,8 @@ def headless(self, value: bool) -> None: warnings.warn( "headless property is deprecated, instead use add_argument('-headless')", DeprecationWarning, stacklevel=2 ) + if not isinstance(value, bool): + raise TypeError("value must be a boolean") if value: self._arguments.append("-headless") elif "-headless" in self._arguments: diff --git a/py/selenium/webdriver/safari/options.py b/py/selenium/webdriver/safari/options.py index 633a7fc19ed63..46ea105abe869 100644 --- a/py/selenium/webdriver/safari/options.py +++ b/py/selenium/webdriver/safari/options.py @@ -59,6 +59,8 @@ def binary_location(self, value: str) -> None: :Args: - value : path to the browser binary """ + if not isinstance(value, str): + raise TypeError("Binary location must be a string") self._binary_location = value def to_capabilities(self) -> dict: @@ -97,6 +99,8 @@ def automatic_inspection(self, value: bool) -> None: :Args: - value: boolean value """ + if not isinstance(value, bool): + raise TypeError("Automatic Inspection must be a boolean") self.set_capability(self.AUTOMATIC_INSPECTION, value) @property @@ -111,6 +115,8 @@ def automatic_profiling(self, value: bool) -> None: :Args: - value: boolean value """ + if not isinstance(value, bool): + raise TypeError("Automatic Profiling must be a boolean") self.set_capability(self.AUTOMATIC_PROFILING, value) @property @@ -126,4 +132,6 @@ def use_technology_preview(self, value: bool) -> None: :Args: - value: boolean value """ + if not isinstance(value, bool): + raise TypeError("Use Technology Preview must be a boolean") self.set_capability("browserName", self.SAFARI_TECH_PREVIEW if value else "safari") diff --git a/py/selenium/webdriver/wpewebkit/options.py b/py/selenium/webdriver/wpewebkit/options.py index 6ad8261360bb5..d92a3853489a0 100644 --- a/py/selenium/webdriver/wpewebkit/options.py +++ b/py/selenium/webdriver/wpewebkit/options.py @@ -34,12 +34,14 @@ def binary_location(self) -> str: return self._binary_location @binary_location.setter - def binary_location(self, value) -> None: + def binary_location(self, value: str) -> None: """Allows you to set the browser binary to launch. :Args: - value : path to the browser binary """ + if not isinstance(value, str): + raise TypeError("Binary Location must be a string") self._binary_location = value def to_capabilities(self): From 63f873b3a9a1fb4e5fbdebb1cc96862f45e63a5c Mon Sep 17 00:00:00 2001 From: sandeepsuryaprasad Date: Sun, 9 Jul 2023 19:21:45 +0530 Subject: [PATCH 2/4] [py] added type check validation in safari/services.py --- py/selenium/webdriver/safari/service.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/py/selenium/webdriver/safari/service.py b/py/selenium/webdriver/safari/service.py index 9ff6179f4fde1..207fe1fcc06b8 100644 --- a/py/selenium/webdriver/safari/service.py +++ b/py/selenium/webdriver/safari/service.py @@ -81,4 +81,6 @@ def reuse_service(self) -> bool: @reuse_service.setter def reuse_service(self, reuse: bool) -> None: + if not isinstance(reuse, bool): + raise TypeError("reuse must be a boolean") self._reuse_service = reuse From ff68ef9adcb063b4d778919ac165b3e94f70743b Mon Sep 17 00:00:00 2001 From: sandeepsuryaprasad Date: Fri, 14 Jul 2023 21:12:48 +0530 Subject: [PATCH 3/4] [py] added cls variable to hold common error message --- py/selenium/webdriver/wpewebkit/options.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/selenium/webdriver/wpewebkit/options.py b/py/selenium/webdriver/wpewebkit/options.py index d92a3853489a0..3b4f3d8680dc1 100644 --- a/py/selenium/webdriver/wpewebkit/options.py +++ b/py/selenium/webdriver/wpewebkit/options.py @@ -41,7 +41,7 @@ def binary_location(self, value: str) -> None: - value : path to the browser binary """ if not isinstance(value, str): - raise TypeError("Binary Location must be a string") + raise TypeError(self.BINARY_LOCATION_ERROR) self._binary_location = value def to_capabilities(self): From 1684568c0b2f6a860a5958fc551241a4fda8abd0 Mon Sep 17 00:00:00 2001 From: sandeepsuryaprasad Date: Fri, 14 Jul 2023 21:15:03 +0530 Subject: [PATCH 4/4] [py] added cls variable to hold common error message --- py/selenium/webdriver/chromium/options.py | 2 +- py/selenium/webdriver/common/options.py | 2 ++ py/selenium/webdriver/firefox/options.py | 2 +- py/selenium/webdriver/safari/options.py | 2 +- 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/py/selenium/webdriver/chromium/options.py b/py/selenium/webdriver/chromium/options.py index d9a00d852d5a9..06c936a6eeec5 100644 --- a/py/selenium/webdriver/chromium/options.py +++ b/py/selenium/webdriver/chromium/options.py @@ -52,7 +52,7 @@ def binary_location(self, value: str) -> None: - value: path to the Chromium binary """ if not isinstance(value, str): - raise TypeError("Binary Location must be a string") + raise TypeError(self.BINARY_LOCATION_ERROR) self._binary_location = value @property diff --git a/py/selenium/webdriver/common/options.py b/py/selenium/webdriver/common/options.py index 6a4bea0801bed..4ea18a6212d8e 100644 --- a/py/selenium/webdriver/common/options.py +++ b/py/selenium/webdriver/common/options.py @@ -227,6 +227,8 @@ def default_capabilities(self): class ArgOptions(BaseOptions): + BINARY_LOCATION_ERROR = "Binary Location Must be a String" + def __init__(self) -> None: super().__init__() self._arguments = [] diff --git a/py/selenium/webdriver/firefox/options.py b/py/selenium/webdriver/firefox/options.py index 82261186c9c67..5d312d0d0c73c 100644 --- a/py/selenium/webdriver/firefox/options.py +++ b/py/selenium/webdriver/firefox/options.py @@ -68,7 +68,7 @@ def binary_location(self) -> str: def binary_location(self, value: str) -> None: """Sets the location of the browser binary by string.""" if not isinstance(value, str): - raise TypeError("Binary Location must be a string") + raise TypeError(self.BINARY_LOCATION_ERROR) self.binary = value @property diff --git a/py/selenium/webdriver/safari/options.py b/py/selenium/webdriver/safari/options.py index 46ea105abe869..86a1d01ee56dd 100644 --- a/py/selenium/webdriver/safari/options.py +++ b/py/selenium/webdriver/safari/options.py @@ -60,7 +60,7 @@ def binary_location(self, value: str) -> None: - value : path to the browser binary """ if not isinstance(value, str): - raise TypeError("Binary location must be a string") + raise TypeError(self.BINARY_LOCATION_ERROR) self._binary_location = value def to_capabilities(self) -> dict: