@@ -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