Skip to content

Commit 492f793

Browse files
committed
Use new API for downloading chromedriver version upper 115
1 parent 1fc1972 commit 492f793

3 files changed

Lines changed: 41 additions & 11 deletions

File tree

combo_e2e/config.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ class Config(BaseConfig):
3838
# chrome driver downloader config (https://chromedriver.chromium.org/)
3939
CHROME_DRIVER_URL = "https://chromedriver.storage.googleapis.com/"
4040
CHROME_DRIVER_VER = "LATEST_RELEASE"
41+
NEW_CHROME_DRIVER_VER = 115
42+
43+
CHROME_DRIVER_LAST_KNOWN_URL = "https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json"
44+
CHROME_DRIVER_PLATFORM = 'linux64'
45+
NEW_CHROME_DRIVER_URL = "https://storage.googleapis.com/chrome-for-testing-public/{version}/{platform}/"
46+
NEW_CHROME_DRIVER_FILE_NAME = "chromedriver-{platform}.zip"
4147
# Major versions of chrome and chrome driver must match
4248
# If you encounter an error: This version of ChromeDriver only supports Chrome version XXX
4349
# then you need to get correct version from https://chromedriver.chromium.org/downloads and enter below
@@ -47,7 +53,7 @@ class Config(BaseConfig):
4753
# if empty string, then the default path in tmp is used
4854
CHROME_DRIVER_PATH = ""
4955
# re download driver every time tests are run
50-
RELOAD_DRIVER = False
56+
RELOAD_DRIVER = True
5157
# kill driver after tests (in case of False browser won't close)
5258
KILL_DRIVER = True
5359
# changes selenium default load timeout

combo_e2e/driver/driver.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
from selenium import webdriver
66
from selenium.webdriver.chrome.options import Options
77
from selenium.webdriver.chrome.service import Service
8+
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
89
from selenium.webdriver.remote import remote_connection
910
from selenium.webdriver.remote.command import Command
10-
from selenium.webdriver.remote.webdriver import WebDriver
11-
from selenium.webdriver.remote.remote_connection import LOGGER
12-
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
1311
from selenium.webdriver.remote.remote_connection import LOGGER
1412
from selenium.webdriver.remote.webdriver import WebDriver
1513

combo_e2e/helpers/chromedriver_loader.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ class ChromeDriverLoader:
4444
"""
4545

4646
driver_name: str = "chromedriver"
47+
zip_driver_path: str = "chromedriver-{platform}"
4748
_path_to_store: Path = None
4849
path_to_download: Path = None
4950
driver_path: str = None
51+
version: str = ""
5052

5153
@classmethod
5254
def download(cls, path_to_download: Path, driver_path: Path) -> None:
@@ -62,7 +64,7 @@ def download(cls, path_to_download: Path, driver_path: Path) -> None:
6264
logger.info(" Starting download chrome driver")
6365
version = cls._get_latest_version()
6466
archive_path = cls._download(version)
65-
cls.driver_path = cls._unzip(archive_path)
67+
cls.driver_path = cls._unzip(archive_path, version=version)
6668
logger.info(
6769
"Prepare tests stage. Chrome driver downloaded and saved in %s",
6870
cls.driver_path,
@@ -98,14 +100,31 @@ def _get(cls, url: str) -> Response:
98100
def _get_latest_version(cls):
99101
version = config.DEFAULT_DRIVER_VER
100102
if not config.DEFAULT_DRIVER_VER:
101-
res = cls._get(urljoin(config.CHROME_DRIVER_URL, config.CHROME_DRIVER_VER))
102-
version = res.content.decode("utf-8").strip()
103+
res = cls._get(config.CHROME_DRIVER_LAST_KNOWN_URL)
104+
try:
105+
versions = res.json()
106+
version = versions.get("channels", {}).get("Stable", {}).get("version")
107+
except Exception as e:
108+
raise ChromeDriverLoaderException(
109+
f"Cannot get chromedriver version. Url: {config.CHROME_DRIVER_LAST_KNOWN_URL}. "
110+
f"Response: {res.text}"
111+
)
103112
return version
104113

114+
@classmethod
115+
def _is_old_version(cls, version: str) -> bool:
116+
major_version = int(version.split(".")[0])
117+
return major_version < config.NEW_CHROME_DRIVER_VER
118+
105119
@classmethod
106120
def _download(cls, version: str) -> Path:
107-
file_relative_path = str(Path(version).joinpath(config.CHROME_DRIVER_FILE_NAME))
108-
download_url = urljoin(config.CHROME_DRIVER_URL, file_relative_path)
121+
if cls._is_old_version(version):
122+
file_relative_path = str(Path(version).joinpath(config.CHROME_DRIVER_FILE_NAME))
123+
download_url = urljoin(config.CHROME_DRIVER_URL, file_relative_path)
124+
else:
125+
file_name = config.NEW_CHROME_DRIVER_FILE_NAME.format(platform=config.CHROME_DRIVER_PLATFORM)
126+
chrome_driver_url = config.NEW_CHROME_DRIVER_URL.format(version=version, platform=config.CHROME_DRIVER_PLATFORM)
127+
download_url = urljoin(chrome_driver_url, file_name)
109128
res = cls._get(download_url)
110129
return cls._save_zip_to_tmp(res.content)
111130

@@ -117,13 +136,20 @@ def _save_zip_to_tmp(cls, data: bytes) -> Path:
117136
return path_to_write
118137

119138
@classmethod
120-
def _unzip(cls, archive_path: Path) -> str:
139+
def _unzip(cls, archive_path: Path, version: str) -> str:
121140
archive = zipfile.ZipFile(file=archive_path)
122141
driver_file_path = cls.make_driver_full_path()
123142
if driver_file_path.exists():
124143
logger.info("Remove previouse driver at: %s", driver_file_path)
125144
driver_file_path.unlink()
126-
archive.extract(cls.driver_name, path=str(cls._path_to_store))
145+
146+
if cls._is_old_version(version):
147+
archive.extract(member=cls.driver_name, path=str(cls._path_to_store))
148+
else:
149+
zip_driver_file_path = f"{cls.zip_driver_path.format(platform=config.CHROME_DRIVER_PLATFORM)}/{cls.driver_name}"
150+
with open(cls._path_to_store.joinpath(cls.driver_name), "wb") as f:
151+
f.write(archive.read(zip_driver_file_path))
152+
127153
archive.close()
128154
if not driver_file_path.exists():
129155
raise ChromeDriverLoaderException(

0 commit comments

Comments
 (0)