From ac03f2168b7e10b613b6fbce32935aa3b0c52370 Mon Sep 17 00:00:00 2001 From: sandeepsuryaprasad Date: Thu, 29 Jun 2023 13:41:11 +0530 Subject: [PATCH 1/3] [py] Moved all Proxytypes to custom descriptor --- py/selenium/webdriver/common/proxy.py | 365 +++++++++++++------------- 1 file changed, 181 insertions(+), 184 deletions(-) diff --git a/py/selenium/webdriver/common/proxy.py b/py/selenium/webdriver/common/proxy.py index fef2365f74f5b..6aff398a48785 100644 --- a/py/selenium/webdriver/common/proxy.py +++ b/py/selenium/webdriver/common/proxy.py @@ -53,6 +53,22 @@ def load(cls, value): raise Exception(f"No proxy type is found for {value}") +class _ProxyTypeDescriptor: + def __init__(self, name, p_type): + self.name = name + self.p_type = p_type + + def __get__(self, obj, cls): + return getattr(obj, self.name) + + def __set__(self, obj, value): + if self.name == "autodetect" and not isinstance(value, bool): + raise ValueError("Autodetect proxy value needs to be a boolean") + getattr(obj, "_verify_proxy_type_compatibility")(self.p_type) + setattr(obj, "proxyType", self.p_type) + setattr(obj, self.name, value) + + class Proxy: """Proxy contains information about proxy type and necessary proxy settings.""" @@ -69,6 +85,155 @@ class Proxy: socksPassword = "" socksVersion = None + # create descriptor type objects + auto_detect = _ProxyTypeDescriptor("autodetect", ProxyType.AUTODETECT) + """Gets and Sets `auto_detect` + + Usage + ----- + - Get + - `self.auto_detect` + - Set + - `self.auto_detect` = `value` + + Parameters + ---------- + `value`: `str` + """ + + ftp_proxy = _ProxyTypeDescriptor("ftpProxy", ProxyType.MANUAL) + """Gets and Sets `ftp_proxy` + + Usage + ----- + - Get + - `self.ftp_proxy` + - Set + - `self.ftp_proxy` = `value` + + Parameters + ---------- + `value`: `str` + """ + http_proxy = _ProxyTypeDescriptor("httpProxy", ProxyType.MANUAL) + """Gets and Sets `http_proxy` + + Usage + ----- + - Get + - `self.http_proxy` + - Set + - `self.http_proxy` = `value` + + Parameters + ---------- + `value`: `str` + """ + + no_proxy = _ProxyTypeDescriptor("noProxy", ProxyType.MANUAL) + """Gets and Sets `no_proxy` + + Usage + ----- + - Get + - `self.no_proxy` + - Set + - `self.no_proxy` = `value` + + Parameters + ---------- + `value`: `str` + """ + + proxy_autoconfig_url = _ProxyTypeDescriptor("proxyAutoconfigUrl", ProxyType.PAC) + """Gets and Sets `proxy_autoconfig_url` + + Usage + ----- + - Get + - `self.proxy_autoconfig_url` + - Set + - `self.proxy_autoconfig_url` = `value` + + Parameter + --------- + `value`: `str` + """ + + ssl_proxy = _ProxyTypeDescriptor("sslProxy", ProxyType.MANUAL) + """Gets and Sets `ssl_proxy` + + Usage + ----- + - Get + - `self.ssl_proxy` + - Set + - `self.ssl_proxy` = `value` + + Parameter + --------- + `value`: `str` + """ + + socks_proxy = _ProxyTypeDescriptor("socksProxy", ProxyType.MANUAL) + """Gets and Sets `socks_proxy` + + Usage + ----- + - Get + - `self.sock_proxy` + - Set + - `self.socks_proxy` = `value` + + Parameter + --------- + `value`: `str` + """ + + socks_username = _ProxyTypeDescriptor("socksUsername", ProxyType.MANUAL) + """Gets and Sets `socks_password` + + Usage + ----- + - Get + - `self.socks_password` + - Set + - `self.socks_password` = `value` + + Parameter + --------- + `value`: `str` + """ + + socks_password = _ProxyTypeDescriptor("socksPassword", ProxyType.MANUAL) + """Gets and Sets `socks_password` + + Usage + ----- + - Get + - `self.socks_password` + - Set + - `self.socks_password` = `value` + + Parameter + --------- + `value`: `str` + """ + socks_version = _ProxyTypeDescriptor("socksVersion", ProxyType.MANUAL) + """Gets and Sets `socks_version` + + Usage + ----- + - Get + - `self.socks_version` + - Set + - `self.socks_version` = `value` + + Parameter + --------- + `value`: `str` + """ + def __init__(self, raw=None): """Creates a new Proxy. @@ -114,170 +279,6 @@ def proxy_type(self, value) -> None: self._verify_proxy_type_compatibility(value) self.proxyType = value - @property - def auto_detect(self): - """Returns autodetect setting.""" - return self.autodetect - - @auto_detect.setter - def auto_detect(self, value) -> None: - """Sets autodetect setting. - - :Args: - - value: The autodetect value. - """ - if isinstance(value, bool): - if self.autodetect is not value: - self._verify_proxy_type_compatibility(ProxyType.AUTODETECT) - self.proxyType = ProxyType.AUTODETECT - self.autodetect = value - else: - raise ValueError("Autodetect proxy value needs to be a boolean") - - @property - def ftp_proxy(self): - """Returns ftp proxy setting.""" - return self.ftpProxy - - @ftp_proxy.setter - def ftp_proxy(self, value) -> None: - """Sets ftp proxy setting. - - :Args: - - value: The ftp proxy value. - """ - self._verify_proxy_type_compatibility(ProxyType.MANUAL) - self.proxyType = ProxyType.MANUAL - self.ftpProxy = value - - @property - def http_proxy(self): - """Returns http proxy setting.""" - return self.httpProxy - - @http_proxy.setter - def http_proxy(self, value) -> None: - """Sets http proxy setting. - - :Args: - - value: The http proxy value. - """ - self._verify_proxy_type_compatibility(ProxyType.MANUAL) - self.proxyType = ProxyType.MANUAL - self.httpProxy = value - - @property - def no_proxy(self): - """Returns noproxy setting.""" - return self.noProxy - - @no_proxy.setter - def no_proxy(self, value) -> None: - """Sets noproxy setting. - - :Args: - - value: The noproxy value. - """ - self._verify_proxy_type_compatibility(ProxyType.MANUAL) - self.proxyType = ProxyType.MANUAL - self.noProxy = value - - @property - def proxy_autoconfig_url(self): - """Returns proxy autoconfig url setting.""" - return self.proxyAutoconfigUrl - - @proxy_autoconfig_url.setter - def proxy_autoconfig_url(self, value) -> None: - """Sets proxy autoconfig url setting. - - :Args: - - value: The proxy autoconfig url value. - """ - self._verify_proxy_type_compatibility(ProxyType.PAC) - self.proxyType = ProxyType.PAC - self.proxyAutoconfigUrl = value - - @property - def ssl_proxy(self): - """Returns https proxy setting.""" - return self.sslProxy - - @ssl_proxy.setter - def ssl_proxy(self, value) -> None: - """Sets https proxy setting. - - :Args: - - value: The https proxy value. - """ - self._verify_proxy_type_compatibility(ProxyType.MANUAL) - self.proxyType = ProxyType.MANUAL - self.sslProxy = value - - @property - def socks_proxy(self): - """Returns socks proxy setting.""" - return self.socksProxy - - @socks_proxy.setter - def socks_proxy(self, value) -> None: - """Sets socks proxy setting. - - :Args: - - value: The socks proxy value. - """ - self._verify_proxy_type_compatibility(ProxyType.MANUAL) - self.proxyType = ProxyType.MANUAL - self.socksProxy = value - - @property - def socks_username(self): - """Returns socks proxy username setting.""" - return self.socksUsername - - @socks_username.setter - def socks_username(self, value) -> None: - """Sets socks proxy username setting. - - :Args: - - value: The socks proxy username value. - """ - self._verify_proxy_type_compatibility(ProxyType.MANUAL) - self.proxyType = ProxyType.MANUAL - self.socksUsername = value - - @property - def socks_password(self): - """Returns socks proxy password setting.""" - return self.socksPassword - - @socks_password.setter - def socks_password(self, value) -> None: - """Sets socks proxy password setting. - - :Args: - - value: The socks proxy password value. - """ - self._verify_proxy_type_compatibility(ProxyType.MANUAL) - self.proxyType = ProxyType.MANUAL - self.socksPassword = value - - @property - def socks_version(self): - """Returns socks proxy version setting.""" - return self.socksVersion - - @socks_version.setter - def socks_version(self, value) -> None: - """Sets socks proxy version setting. - - :Args: - - value: The socks proxy version value. - """ - self._verify_proxy_type_compatibility(ProxyType.MANUAL) - self.proxyType = ProxyType.MANUAL - self.socksVersion = value - def _verify_proxy_type_compatibility(self, compatible_proxy): if self.proxyType not in (ProxyType.UNSPECIFIED, compatible_proxy): raise Exception( @@ -286,24 +287,20 @@ def _verify_proxy_type_compatibility(self, compatible_proxy): def to_capabilities(self): proxy_caps = {"proxyType": self.proxyType["string"].lower()} - if self.autodetect: - proxy_caps["autodetect"] = self.autodetect - if self.ftpProxy: - proxy_caps["ftpProxy"] = self.ftpProxy - if self.httpProxy: - proxy_caps["httpProxy"] = self.httpProxy - if self.proxyAutoconfigUrl: - proxy_caps["proxyAutoconfigUrl"] = self.proxyAutoconfigUrl - if self.sslProxy: - proxy_caps["sslProxy"] = self.sslProxy - if self.noProxy: - proxy_caps["noProxy"] = self.noProxy - if self.socksProxy: - proxy_caps["socksProxy"] = self.socksProxy - if self.socksUsername: - proxy_caps["socksUsername"] = self.socksUsername - if self.socksPassword: - proxy_caps["socksPassword"] = self.socksPassword - if self.socksVersion: - proxy_caps["socksVersion"] = self.socksVersion + proxies = [ + "autodetect", + "ftpProxy", + "httpProxy", + "proxyAutoconfigUrl", + "sslProxy", + "noProxy", + "socksProxy", + "socksUsername", + "socksPassword", + "socksVersion", + ] + for proxy in proxies: + attr_value = getattr(self, proxy) + if attr_value: + proxy_caps[proxy] = attr_value return proxy_caps From 311dc414487aeaf526c6d69ebbb7bcbf0ee8ca58 Mon Sep 17 00:00:00 2001 From: sandeepsuryaprasad Date: Thu, 29 Jun 2023 13:43:49 +0530 Subject: [PATCH 2/3] [py] Moved all Proxytypes to custom descriptor --- py/selenium/webdriver/common/proxy.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/py/selenium/webdriver/common/proxy.py b/py/selenium/webdriver/common/proxy.py index 6aff398a48785..467651d2070c5 100644 --- a/py/selenium/webdriver/common/proxy.py +++ b/py/selenium/webdriver/common/proxy.py @@ -115,6 +115,7 @@ class Proxy: ---------- `value`: `str` """ + http_proxy = _ProxyTypeDescriptor("httpProxy", ProxyType.MANUAL) """Gets and Sets `http_proxy` @@ -219,6 +220,7 @@ class Proxy: --------- `value`: `str` """ + socks_version = _ProxyTypeDescriptor("socksVersion", ProxyType.MANUAL) """Gets and Sets `socks_version` From ca1b5764d480f44cf1beb09a8429e3ec45ad078e Mon Sep 17 00:00:00 2001 From: sandeepsuryaprasad Date: Tue, 4 Jul 2023 15:47:08 +0530 Subject: [PATCH 3/3] 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..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: - return DesiredCapabilities.INTERNETEXPLORER.copy() \ No newline at end of file + return DesiredCapabilities.INTERNETEXPLORER.copy()