Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix chrome for test chromedriver install #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/webdrivermanager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,6 +15,7 @@
"firefox": GeckoDriverManager,
"gecko": GeckoDriverManager,
"mozilla": GeckoDriverManager,
"chrometest": ChromeDriverForTest,
"edge": EdgeDriverManager,
"edgechromium": EdgeChromiumDriverManager,
"ie": IEDriverManager,
Expand All @@ -25,6 +27,7 @@
"GeckoDriverManager",
"EdgeDriverManager",
"IEDriverManager",
"ChromeDriverForTest"
"EdgeChromiumDriverManager",
"get_version",
"AVAILABLE_DRIVERS",
Expand Down
6 changes: 5 additions & 1 deletion src/webdrivermanager/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/webdrivermanager/chrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
135 changes: 135 additions & 0 deletions src/webdrivermanager/chrometest.py
Original file line number Diff line number Diff line change
@@ -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")

13 changes: 13 additions & 0 deletions src/webdrivermanager/common_chrome.py
Original file line number Diff line number Diff line change
@@ -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.")