From e45828a1df4a1cf1f82d82ebf3edc0b1d743fbe6 Mon Sep 17 00:00:00 2001 From: sandeepsuryaprasad Date: Tue, 15 Aug 2023 16:01:58 +0530 Subject: [PATCH] [py] moved get_path method to SeleniumManager class --- py/selenium/webdriver/chromium/webdriver.py | 4 +- py/selenium/webdriver/common/driver_finder.py | 46 ------------------- .../webdriver/common/selenium_manager.py | 18 ++++++++ py/selenium/webdriver/firefox/webdriver.py | 4 +- py/selenium/webdriver/ie/webdriver.py | 4 +- py/selenium/webdriver/safari/webdriver.py | 4 +- py/selenium/webdriver/webkitgtk/webdriver.py | 4 +- py/selenium/webdriver/wpewebkit/webdriver.py | 4 +- .../common/selenium_manager_tests.py | 3 +- 9 files changed, 31 insertions(+), 60 deletions(-) delete mode 100644 py/selenium/webdriver/common/driver_finder.py diff --git a/py/selenium/webdriver/chromium/webdriver.py b/py/selenium/webdriver/chromium/webdriver.py index 71f127dd0b8aa..7487c021bef97 100644 --- a/py/selenium/webdriver/chromium/webdriver.py +++ b/py/selenium/webdriver/chromium/webdriver.py @@ -16,8 +16,8 @@ # under the License. from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection -from selenium.webdriver.common.driver_finder import DriverFinder from selenium.webdriver.common.options import ArgOptions +from selenium.webdriver.common.selenium_manager import SeleniumManager from selenium.webdriver.common.service import Service from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver @@ -48,7 +48,7 @@ def __init__( self.service = service - self.service.path = DriverFinder.get_path(self.service, options) + self.service.path = SeleniumManager.get_path(self.service, options) self.service.start() diff --git a/py/selenium/webdriver/common/driver_finder.py b/py/selenium/webdriver/common/driver_finder.py deleted file mode 100644 index 6aa5f96eef01d..0000000000000 --- a/py/selenium/webdriver/common/driver_finder.py +++ /dev/null @@ -1,46 +0,0 @@ -# Licensed to the Software Freedom Conservancy (SFC) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The SFC licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. -import logging -from pathlib import Path - -from selenium.common.exceptions import NoSuchDriverException -from selenium.webdriver.common.options import BaseOptions -from selenium.webdriver.common.selenium_manager import SeleniumManager -from selenium.webdriver.common.service import Service - -logger = logging.getLogger(__name__) - - -class DriverFinder: - """Utility to find if a given file is present and executable. - - This implementation is still in beta, and may change. - """ - - @staticmethod - def get_path(service: Service, options: BaseOptions) -> str: - path = service.path - try: - path = SeleniumManager().driver_location(options) if path is None else path - except Exception as err: - msg = f"Unable to obtain driver for {options.capabilities['browserName']} using Selenium Manager." - raise NoSuchDriverException(msg) from err - - if path is None or not Path(path).is_file(): - raise NoSuchDriverException(f"Unable to locate or obtain driver for {options.capabilities['browserName']}") - - return path diff --git a/py/selenium/webdriver/common/selenium_manager.py b/py/selenium/webdriver/common/selenium_manager.py index af3d186ba47b1..6ef6c43c09752 100644 --- a/py/selenium/webdriver/common/selenium_manager.py +++ b/py/selenium/webdriver/common/selenium_manager.py @@ -21,8 +21,10 @@ from pathlib import Path from typing import List +from selenium.common import NoSuchDriverException from selenium.common import WebDriverException from selenium.webdriver.common.options import BaseOptions +from selenium.webdriver.common.service import Service logger = logging.getLogger(__name__) @@ -99,6 +101,22 @@ def driver_location(self, options: BaseOptions) -> str: return driver_path + @staticmethod + def get_path(service: Service, options: BaseOptions) -> str: + """Returns the path of the driver if it is present and executable.""" + + path = service.path + try: + path = SeleniumManager().driver_location(options) if path is None else path + except Exception as err: + msg = f"Unable to obtain driver for {options.capabilities['browserName']} using Selenium Manager." + raise NoSuchDriverException(msg) from err + + if path is None or not Path(path).is_file(): + raise NoSuchDriverException(f"Unable to locate or obtain driver for {options.capabilities['browserName']}") + + return path + @staticmethod def run(args: List[str]) -> dict: """ diff --git a/py/selenium/webdriver/firefox/webdriver.py b/py/selenium/webdriver/firefox/webdriver.py index 8d30f5c8b417d..514f8b0741956 100644 --- a/py/selenium/webdriver/firefox/webdriver.py +++ b/py/selenium/webdriver/firefox/webdriver.py @@ -22,7 +22,7 @@ from contextlib import contextmanager from io import BytesIO -from selenium.webdriver.common.driver_finder import DriverFinder +from selenium.webdriver.common.selenium_manager import SeleniumManager from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver from .options import Options @@ -56,7 +56,7 @@ def __init__( self.service = service if service else Service() options = options if options else Options() - self.service.path = DriverFinder.get_path(self.service, options) + self.service.path = SeleniumManager.get_path(self.service, options) self.service.start() executor = FirefoxRemoteConnection( diff --git a/py/selenium/webdriver/ie/webdriver.py b/py/selenium/webdriver/ie/webdriver.py index 422a2e87b40aa..fbac6cb9a9233 100644 --- a/py/selenium/webdriver/ie/webdriver.py +++ b/py/selenium/webdriver/ie/webdriver.py @@ -15,7 +15,7 @@ # specific language governing permissions and limitations # under the License. -from selenium.webdriver.common.driver_finder import DriverFinder +from selenium.webdriver.common.selenium_manager import SeleniumManager from selenium.webdriver.remote.remote_connection import RemoteConnection from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver @@ -46,7 +46,7 @@ def __init__( self.service = service if service else Service() options = options if options else Options() - self.service.path = DriverFinder.get_path(self.service, options) + self.service.path = SeleniumManager.get_path(self.service, options) self.service.start() executor = RemoteConnection( diff --git a/py/selenium/webdriver/safari/webdriver.py b/py/selenium/webdriver/safari/webdriver.py index 90fd755ba47ec..9c14d2d9248be 100644 --- a/py/selenium/webdriver/safari/webdriver.py +++ b/py/selenium/webdriver/safari/webdriver.py @@ -21,7 +21,7 @@ from selenium.common.exceptions import WebDriverException from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver -from ..common.driver_finder import DriverFinder +from ..common.selenium_manager import SeleniumManager from .options import Options from .remote_connection import SafariRemoteConnection from .service import Service @@ -57,7 +57,7 @@ def __init__( self.service = service if service else Service() options = options if options else Options() - self.service.path = DriverFinder.get_path(self.service, options) + self.service.path = SeleniumManager.get_path(self.service, options) self._reuse_service = reuse_service and self.service.reuse_service if not self._reuse_service: diff --git a/py/selenium/webdriver/webkitgtk/webdriver.py b/py/selenium/webdriver/webkitgtk/webdriver.py index b364ca5f192be..e5fef9f7be51a 100644 --- a/py/selenium/webdriver/webkitgtk/webdriver.py +++ b/py/selenium/webdriver/webkitgtk/webdriver.py @@ -17,7 +17,7 @@ import http.client as http_client -from selenium.webdriver.common.driver_finder import DriverFinder +from selenium.webdriver.common.selenium_manager import SeleniumManager from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver from .options import Options @@ -60,7 +60,7 @@ def __init__( desired_capabilities = capabilities self.service = Service(executable_path, port=port, log_path=service_log_path) - self.service.path = DriverFinder.get_path(self.service, options) + self.service.path = SeleniumManager.get_path(self.service, options) self.service.start() super().__init__( diff --git a/py/selenium/webdriver/wpewebkit/webdriver.py b/py/selenium/webdriver/wpewebkit/webdriver.py index fe98ba6907c63..c57d5d59a6799 100644 --- a/py/selenium/webdriver/wpewebkit/webdriver.py +++ b/py/selenium/webdriver/wpewebkit/webdriver.py @@ -18,7 +18,7 @@ import http.client as http_client from selenium.webdriver.common.desired_capabilities import DesiredCapabilities -from selenium.webdriver.common.driver_finder import DriverFinder +from selenium.webdriver.common.selenium_manager import SeleniumManager from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver from .options import Options @@ -56,7 +56,7 @@ def __init__( options = Options() self.service = Service(executable_path, port=port, log_path=service_log_path) - self.service.path = DriverFinder.get_path(self.service, options) + self.service.path = SeleniumManager.get_path(self.service, options) self.service.start() super().__init__(command_executor=self.service.service_url, desired_capabilities=desired_capabilities) diff --git a/py/test/selenium/webdriver/common/selenium_manager_tests.py b/py/test/selenium/webdriver/common/selenium_manager_tests.py index db637af275b9b..c79bfe397ffb9 100644 --- a/py/test/selenium/webdriver/common/selenium_manager_tests.py +++ b/py/test/selenium/webdriver/common/selenium_manager_tests.py @@ -22,7 +22,6 @@ from selenium.common.exceptions import WebDriverException from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service -from selenium.webdriver.common.driver_finder import DriverFinder from selenium.webdriver.common.proxy import Proxy from selenium.webdriver.common.selenium_manager import SeleniumManager @@ -110,4 +109,4 @@ def test_driver_finder_error(mocker): options = Options() msg = r"Unable to locate or obtain driver for chrome.*errors\/driver_location" with pytest.raises(WebDriverException, match=msg): - DriverFinder.get_path(service, options) + SeleniumManager.get_path(service, options)