From b5b3f085a49b7194bee4b66df604a46924f247fe Mon Sep 17 00:00:00 2001 From: titusfortner Date: Thu, 13 Jul 2023 20:11:50 -0500 Subject: [PATCH] [py] have superclass always create remote connection --- .../webdriver/chrome/remote_connection.py | 11 +++- py/selenium/webdriver/chrome/webdriver.py | 9 +--- py/selenium/webdriver/chromium/webdriver.py | 50 +++++++++++-------- .../webdriver/edge/remote_connection.py | 11 +++- py/selenium/webdriver/edge/webdriver.py | 11 +--- py/selenium/webdriver/firefox/webdriver.py | 20 ++++---- py/selenium/webdriver/ie/webdriver.py | 24 +++++---- py/selenium/webdriver/safari/webdriver.py | 20 +++----- 8 files changed, 84 insertions(+), 72 deletions(-) diff --git a/py/selenium/webdriver/chrome/remote_connection.py b/py/selenium/webdriver/chrome/remote_connection.py index 582fbe4064819..f289ff1f5e400 100644 --- a/py/selenium/webdriver/chrome/remote_connection.py +++ b/py/selenium/webdriver/chrome/remote_connection.py @@ -16,14 +16,23 @@ # under the License. import typing +from selenium.webdriver import DesiredCapabilities from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection class ChromeRemoteConnection(ChromiumRemoteConnection): + browser_name = DesiredCapabilities.CHROME["browserName"] + def __init__( self, remote_server_addr: str, keep_alive: bool = True, ignore_proxy: typing.Optional[bool] = False, ) -> None: - super().__init__(remote_server_addr, 'goog', keep_alive, ignore_proxy=ignore_proxy) + super().__init__( + remote_server_addr=remote_server_addr, + vendor_prefix="goog", + browser_name="chrome", + keep_alive=keep_alive, + ignore_proxy=ignore_proxy, + ) diff --git a/py/selenium/webdriver/chrome/webdriver.py b/py/selenium/webdriver/chrome/webdriver.py index e6795a5884eec..517f14a1d7097 100644 --- a/py/selenium/webdriver/chrome/webdriver.py +++ b/py/selenium/webdriver/chrome/webdriver.py @@ -16,7 +16,6 @@ # under the License. from selenium.webdriver.chromium.webdriver import ChromiumDriver -from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from .options import Options from .service import Service @@ -42,10 +41,4 @@ def __init__( service = service if service else Service() options = options if options else Options() - super().__init__( - DesiredCapabilities.CHROME["browserName"], - "goog", - options, - service, - keep_alive, - ) + super().__init__(options=options, service=service, keep_alive=keep_alive) diff --git a/py/selenium/webdriver/chromium/webdriver.py b/py/selenium/webdriver/chromium/webdriver.py index 71f127dd0b8aa..0c12cad51c3ea 100644 --- a/py/selenium/webdriver/chromium/webdriver.py +++ b/py/selenium/webdriver/chromium/webdriver.py @@ -14,6 +14,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +import warnings from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection from selenium.webdriver.common.driver_finder import DriverFinder @@ -28,11 +29,12 @@ class ChromiumDriver(RemoteWebDriver): def __init__( self, - browser_name, - vendor_prefix, - options: ArgOptions, - service: Service, - keep_alive=True, + browser_name: str = None, + vendor_prefix: str = None, + options: ArgOptions = ArgOptions(), + service: Service = None, + keep_alive: bool = True, + remote_connection: ChromiumRemoteConnection = None, ) -> None: """Creates a new WebDriver instance of the ChromiumDriver. Starts the service and then creates new WebDriver instance of ChromiumDriver. @@ -44,25 +46,34 @@ def __init__( - service - Service object for handling the browser driver if you need to pass extra details - keep_alive - Whether to configure ChromiumRemoteConnection to use HTTP keep-alive. """ - self.vendor_prefix = vendor_prefix - self.service = service + if browser_name: + warnings.warn( + "browser_name is not necessary when an Options class is used", DeprecationWarning, stacklevel=2 + ) + self.service = service self.service.path = DriverFinder.get_path(self.service, options) - self.service.start() - try: - super().__init__( - command_executor=ChromiumRemoteConnection( - remote_server_addr=self.service.service_url, - browser_name=browser_name, - vendor_prefix=vendor_prefix, - keep_alive=keep_alive, - ignore_proxy=options._ignore_local_proxy, - ), - options=options, + if vendor_prefix: + warnings.warn( + "vendor_prefix is deprecated, use a ChromiumRemoteConnection with command_executor instead", + DeprecationWarning, + stacklevel=2, ) + remote_connection = ChromiumRemoteConnection( + remote_server_addr=self.service.service_url, + browser_name=browser_name, + vendor_prefix=vendor_prefix, + keep_alive=keep_alive, + ignore_proxy=options._ignore_local_proxy, + ) + + command_executor = remote_connection if remote_connection else self.service.service_url + + try: + super().__init__(command_executor=command_executor, options=options, keep_alive=keep_alive) except Exception: self.quit() raise @@ -185,8 +196,7 @@ def stop_casting(self, sink_name: str) -> dict: return self.execute("stopCasting", {"sinkName": sink_name}) def quit(self) -> None: - """Closes the browser and shuts down the ChromiumDriver executable that - is started when starting the ChromiumDriver.""" + """Ends the driver session and shuts down the ChromiumDriver executable.""" try: super().quit() except Exception: diff --git a/py/selenium/webdriver/edge/remote_connection.py b/py/selenium/webdriver/edge/remote_connection.py index 858ddb1759552..c91fb030a5da1 100644 --- a/py/selenium/webdriver/edge/remote_connection.py +++ b/py/selenium/webdriver/edge/remote_connection.py @@ -16,14 +16,23 @@ # under the License. import typing +from selenium.webdriver import DesiredCapabilities from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection class EdgeRemoteConnection(ChromiumRemoteConnection): + browser_name = DesiredCapabilities.EDGE["browserName"] + def __init__( self, remote_server_addr: str, keep_alive: bool = True, ignore_proxy: typing.Optional[bool] = False, ) -> None: - super().__init__(remote_server_addr, 'ms', keep_alive, ignore_proxy=ignore_proxy) + super().__init__( + remote_server_addr=remote_server_addr, + vendor_prefix="goog", + browser_name="MicrosoftEdge", + keep_alive=keep_alive, + ignore_proxy=ignore_proxy, + ) diff --git a/py/selenium/webdriver/edge/webdriver.py b/py/selenium/webdriver/edge/webdriver.py index 1d0410a79ae44..6fc1116e894a2 100644 --- a/py/selenium/webdriver/edge/webdriver.py +++ b/py/selenium/webdriver/edge/webdriver.py @@ -16,7 +16,6 @@ # under the License. from selenium.webdriver.chromium.webdriver import ChromiumDriver -from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from .options import Options from .service import Service @@ -29,7 +28,7 @@ def __init__( self, options: Options = None, service: Service = None, - keep_alive=True, + keep_alive: bool = True, ) -> None: """Creates a new instance of the edge driver. Starts the service and then creates new instance of edge driver. @@ -42,10 +41,4 @@ def __init__( service = service if service else Service() options = options if options else Options() - super().__init__( - DesiredCapabilities.EDGE["browserName"], - "ms", - options, - service, - keep_alive, - ) + super().__init__(options=options, service=service, keep_alive=keep_alive) diff --git a/py/selenium/webdriver/firefox/webdriver.py b/py/selenium/webdriver/firefox/webdriver.py index 371d6c0f97d62..e0121dfa9e6ba 100644 --- a/py/selenium/webdriver/firefox/webdriver.py +++ b/py/selenium/webdriver/firefox/webdriver.py @@ -26,7 +26,6 @@ from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver from .options import Options -from .remote_connection import FirefoxRemoteConnection from .service import Service logger = logging.getLogger(__name__) @@ -42,7 +41,7 @@ def __init__( self, options: Options = None, service: Service = None, - keep_alive=True, + keep_alive: bool = True, ) -> None: """Creates a new instance of the Firefox driver. Starts the service and then creates new instance of Firefox driver. @@ -59,24 +58,23 @@ def __init__( self.service.path = DriverFinder.get_path(self.service, options) self.service.start() - executor = FirefoxRemoteConnection( - remote_server_addr=self.service.service_url, - ignore_proxy=options._ignore_local_proxy, - keep_alive=keep_alive, - ) - super().__init__(command_executor=executor, options=options) + try: + super().__init__(command_executor=self.service.service_url, options=options, keep_alive=keep_alive) + except Exception: + self.quit() + raise self._is_remote = False def quit(self) -> None: - """Quits the driver and close every associated window.""" + """Ends the driver session and shuts down the geckodriver executable.""" try: super().quit() except Exception: # We don't care about the message because something probably has gone wrong pass - - self.service.stop() + finally: + self.service.stop() def set_context(self, context) -> None: self.execute("SET_CONTEXT", {"context": context}) diff --git a/py/selenium/webdriver/ie/webdriver.py b/py/selenium/webdriver/ie/webdriver.py index 422a2e87b40aa..def009d4c6710 100644 --- a/py/selenium/webdriver/ie/webdriver.py +++ b/py/selenium/webdriver/ie/webdriver.py @@ -16,7 +16,6 @@ # under the License. from selenium.webdriver.common.driver_finder import DriverFinder -from selenium.webdriver.remote.remote_connection import RemoteConnection from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver from .options import Options @@ -49,15 +48,20 @@ def __init__( self.service.path = DriverFinder.get_path(self.service, options) self.service.start() - executor = RemoteConnection( - remote_server_addr=self.service.service_url, - keep_alive=keep_alive, - ignore_proxy=options._ignore_local_proxy, - ) + try: + super().__init__(command_executor=self.service.service_url, options=options, keep_alive=keep_alive) + except Exception: + self.quit() + raise - super().__init__(command_executor=executor, options=options) self._is_remote = False - def quit(self) -> None: - super().quit() - self.service.stop() + def quit(self): + """Ends the driver session and shuts down the safaridriver executable if necessary.""" + try: + super().quit() + except Exception: + # We don't care about the message because something probably has gone wrong + pass + finally: + self.service.stop() diff --git a/py/selenium/webdriver/safari/webdriver.py b/py/selenium/webdriver/safari/webdriver.py index 9c67ea936bff1..d29b1521a9246 100644 --- a/py/selenium/webdriver/safari/webdriver.py +++ b/py/selenium/webdriver/safari/webdriver.py @@ -15,7 +15,6 @@ # specific language governing permissions and limitations # under the License. -import http.client as http_client import warnings from selenium.common.exceptions import WebDriverException @@ -24,7 +23,6 @@ from ..common.driver_finder import DriverFinder from .options import Options -from .remote_connection import SafariRemoteConnection from .service import Service DEFAULT_SAFARI_CAPS = DesiredCapabilities.SAFARI.copy() @@ -66,22 +64,20 @@ def __init__( if not self._reuse_service: self.service.start() - executor = SafariRemoteConnection( - remote_server_addr=self.service.service_url, - keep_alive=keep_alive, - ignore_proxy=options._ignore_local_proxy, - ) - - super().__init__(command_executor=executor, options=options) + try: + super().__init__(command_executor=self.service.service_url, options=options, keep_alive=keep_alive) + except Exception: + self.quit() + raise self._is_remote = False def quit(self): - """Closes the browser and shuts down the SafariDriver executable that - is started when starting the SafariDriver.""" + """Ends the driver session and shuts down the safaridriver executable if necessary.""" try: super().quit() - except http_client.BadStatusLine: + except Exception: + # We don't care about the message because something probably has gone wrong pass finally: if not self._reuse_service: