From e925807431498b15c5efcdb3bec1ffd754787791 Mon Sep 17 00:00:00 2001 From: Sidhant Date: Sun, 26 Nov 2023 13:36:00 +0530 Subject: [PATCH] fix chrome for test chromedriver install --- src/webdrivermanager/__init__.py | 3 + src/webdrivermanager/base.py | 6 +- src/webdrivermanager/chrome.py | 3 +- src/webdrivermanager/chrometest.py | 135 ++++++++++++++++++++++++++ src/webdrivermanager/common_chrome.py | 13 +++ 5 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 src/webdrivermanager/chrometest.py create mode 100644 src/webdrivermanager/common_chrome.py diff --git a/src/webdrivermanager/__init__.py b/src/webdrivermanager/__init__.py index 2374510..aebecfa 100644 --- a/src/webdrivermanager/__init__.py +++ b/src/webdrivermanager/__init__.py @@ -2,6 +2,7 @@ from .base import WebDriverManagerBase from .chrome import ChromeDriverManager +from .chrometest import ChromeDriverForTest from .gecko import GeckoDriverManager from .edge import EdgeDriverManager from .ie import IEDriverManager @@ -14,6 +15,7 @@ "firefox": GeckoDriverManager, "gecko": GeckoDriverManager, "mozilla": GeckoDriverManager, + "chrometest": ChromeDriverForTest, "edge": EdgeDriverManager, "edgechromium": EdgeChromiumDriverManager, "ie": IEDriverManager, @@ -25,6 +27,7 @@ "GeckoDriverManager", "EdgeDriverManager", "IEDriverManager", + "ChromeDriverForTest" "EdgeChromiumDriverManager", "get_version", "AVAILABLE_DRIVERS", diff --git a/src/webdrivermanager/base.py b/src/webdrivermanager/base.py index 7a18df7..8927976 100644 --- a/src/webdrivermanager/base.py +++ b/src/webdrivermanager/base.py @@ -239,8 +239,10 @@ def download(self, version="latest", show_progress_bar=True, force=False): :returns: The path + filename to the downloaded web driver binary. """ (download_url, filename) = self.get_download_url(version) - + print(filename) + print("dwnld_url", download_url) dl_path = Path(self.get_download_path(version)) + print("dl_path", dl_path) filename_with_path = dl_path / filename dl_path.mkdir(parents=True, exist_ok=True) if filename_with_path.exists(): @@ -313,11 +315,13 @@ def download_and_install(self, version="latest", show_progress_bar=True): try: archive_file = dl_path / filename + print("archivetype", archive_file) if archive_type == 1: with tarfile.open(archive_file, mode="r:*") as tar: tar.extractall(extract_dir) LOGGER.debug("Extracted files: %s", ", ".join(tar.getnames())) elif archive_type == 2: + with zipfile.ZipFile(archive_file, mode="r") as driver_zipfile: driver_zipfile.extractall(extract_dir) # TODO: Get filenames and log debug diff --git a/src/webdrivermanager/chrome.py b/src/webdrivermanager/chrome.py index 454a7b9..3650402 100644 --- a/src/webdrivermanager/chrome.py +++ b/src/webdrivermanager/chrome.py @@ -3,10 +3,11 @@ import re from pathlib import Path from .base import WebDriverManagerBase +from .common_chrome import CommonWebDriverManager from .misc import LOGGER, raise_runtime_error, get_output -class ChromeDriverManager(WebDriverManagerBase): +class ChromeDriverManager(CommonWebDriverManager): """Class for downloading the Google Chrome WebDriver.""" chrome_driver_base_url = "https://www.googleapis.com/storage/v1/b/chromedriver" diff --git a/src/webdrivermanager/chrometest.py b/src/webdrivermanager/chrometest.py new file mode 100644 index 0000000..b91f1ef --- /dev/null +++ b/src/webdrivermanager/chrometest.py @@ -0,0 +1,135 @@ +# -*- coding: utf-8 -*- +import requests +import re +from pathlib import Path +from .base import WebDriverManagerBase +from .common_chrome import CommonWebDriverManager +from .misc import LOGGER, raise_runtime_error, get_output +import json +import sys +import platform + + + +class ChromeDriverForTest(CommonWebDriverManager): + json_url_for_binaries = "https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json" + + driver_filenames = { + "win": "chromedriver.exe", + "mac": "chromedriver", + "linux": "chromedriver", + } + + chrome_version_pattern = r"(\d+\.\d+.\d+)(\.\d+)" + + chrome_version_commands = { + "win": [ + ["reg", "query", r"HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon", "/v", "version"], + ["reg", "query", r"HKEY_CURRENT_USER\Software\Chromium\BLBeacon", "/v", "version"], + ], + "linux": [ + ["chromium", "--version"], + ["chromium-browser", "--version"], + ["google-chrome", "--version"], + ], + "mac": [ + ["/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", "--version"], + ["/Applications/Chromium.app/Contents/MacOS/Chromium", "--version"], + ], + } + + def get_download_path(self, version="latest"): + json_data = self.fetch_chromedriver_json_data() + target_channel="Stable" + if json_data is None: + LOGGER.error("Failed to fetch chromedriver JSON data.") + return None + json_obj = json.loads(json_data) + target_channel="Stable" + channels = json_obj.get("channels", {}) + if target_channel in channels: + target_channel_info = channels.get(target_channel, {}) + version = target_channel_info.get("version", "") + print(version) + return self.download_root / "chrome" / version + + def get_download_url(self, version="latest"): + target_version = self._get_browser_version().split('.')[0] + print(target_version) + json_data = self.fetch_chromedriver_json_data() + target_channel="Stable" + if json_data is None: + LOGGER.error("Failed to fetch chromedriver JSON data.") + return None + json_obj = json.loads(json_data) + channels = json_obj.get("channels", {}) + LOGGER.debug(f"Channels: {channels}") + print("got it sidhant") + print("channel:", channels) + print("target channel:",target_channel) + if target_channel in channels: + target_channel_info = channels.get(target_channel, {}) + version = target_channel_info.get("version", "") + print("version", version) + print("target channel ifo",target_channel_info) + target_version_channel = target_channel_info.get("version", "").split('.')[0] + print(target_version_channel) + downloads = target_channel_info.get("downloads", {}) + print("target info", target_version) + if target_version_channel == target_version: + os_platform = sys.platform.lower() + if os_platform == "darwin": + os_platform = self.get_architecture() + filename = "chromedriver-" + os_platform + ".zip" + print("os",os_platform) + chromedriver_urls = downloads.get("chromedriver", []) + for entry in chromedriver_urls: + print("entry",entry) + if entry.get("platform", "").lower() == os_platform: + print(entry.get("url", ""), version) + return entry.get("url", ""), filename + LOGGER.warning("No valid download URL found for chromedriver.") + return None + + + def get_architecture(self): + arch = platform.architecture()[0] + system = platform.system() + + if system == "Darwin": + # macOS + if arch == "64bit": + return "mac-arm64" if "arm" in platform.processor().lower() else "mac-x64" + return "Unknown" + + def fetch_chromedriver_json_data(self): + url = "https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json" + response = requests.get(url) + if response.status_code == 200: + jsondata = json.dumps(response.json()) + return jsondata + else: + print(f"Error fetching data. Status code: {response.status_code}") + return None + + + + def _get_browser_version(self): + commands = self.chrome_version_commands.get(self.os_name) + if not commands: + raise NotImplementedError("Unsupported system: %s", self.os_name) + + for cmd in commands: + output = get_output(cmd) + if not output: + continue + + version = re.search(self.chrome_version_pattern, output) + print(version) + if not version: + continue + + return version.group(1) + + raise RuntimeError("Unable to read current browser version") + diff --git a/src/webdrivermanager/common_chrome.py b/src/webdrivermanager/common_chrome.py new file mode 100644 index 0000000..99fa6e9 --- /dev/null +++ b/src/webdrivermanager/common_chrome.py @@ -0,0 +1,13 @@ +# common_base.py +from .base import WebDriverManagerBase + +class CommonWebDriverManager(WebDriverManagerBase): + json_url_for_binaries = "https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json" + driver_filenames = { + "win": "chromedriver.exe", + "mac": "chromedriver", + "linux": "chromedriver", + } + + def get_download_url(self, version="latest"): + raise NotImplementedError("get_download_url must be implemented in the subclass.")