forked from mherrmann/helium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto-install chromedriver from start_chrome(...)
- Loading branch information
Showing
9 changed files
with
91 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from get_chrome_driver import GetChromeDriver | ||
from os.path import join, expanduser | ||
|
||
import os | ||
import platform | ||
|
||
def install_matching_chromedriver(): | ||
get_driver = GetChromeDriver() | ||
chrome_version = get_driver._GetChromeDriver__get_installed_chrome_version() | ||
cache_directory = _get_cache_directory() | ||
cached_chrome_version = \ | ||
StringStoredInFile(join(cache_directory, 'chrome.version')) | ||
if chrome_version != cached_chrome_version.read(): | ||
get_driver.auto_download(cache_directory, True) | ||
cached_chrome_version.write(chrome_version) | ||
return join( | ||
cache_directory, | ||
'chromedriver' + '.exe' if platform.system() == 'Windows' else '' | ||
) | ||
|
||
def _get_cache_directory(): | ||
system = platform.system() | ||
if system == 'Windows': | ||
return join(os.getenv('LOCALAPPDATA'), 'Cache', 'Helium') | ||
elif system == 'Darwin': | ||
return expanduser('~/Library/Caches/Helium') | ||
else: | ||
cache_home = os.getenv('XDG_CACHE_HOME', expanduser('~/.cache')) | ||
return join(cache_home, 'helium') | ||
|
||
class StringStoredInFile: | ||
def __init__(self, path): | ||
self.path = path | ||
def read(self): | ||
try: | ||
with open(self.path) as f: | ||
return f.read() | ||
except FileNotFoundError: | ||
return None | ||
def write(self, value): | ||
with open(self.path, 'w') as f: | ||
f.write(value) |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,6 @@ | |
selenium==3.141.0 | ||
|
||
# Selenium 3 is incompatible with urllib3 >= 2: | ||
urllib3<2 | ||
urllib3<2 | ||
|
||
get-chrome-driver==1.3.12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from helium._impl import chromedriver | ||
from helium._impl.chromedriver import install_matching_chromedriver | ||
from os import access, X_OK | ||
from os.path import getctime | ||
from tempfile import TemporaryDirectory | ||
from unittest import TestCase | ||
|
||
|
||
class InstallMatchingChromeDriverTest(TestCase): | ||
def test_install_matching_chromedriver(self): | ||
driver_path = install_matching_chromedriver() | ||
self.assertTrue(access(driver_path, X_OK)) | ||
def test_caching(self): | ||
driver_path = install_matching_chromedriver() | ||
ctime = getctime(driver_path) | ||
self.assertEqual(install_matching_chromedriver(), driver_path) | ||
self.assertEqual(ctime, getctime(driver_path)) | ||
def setUp(self): | ||
self.temp_dir = TemporaryDirectory() | ||
self._original_get_cache_directory = chromedriver._get_cache_directory | ||
chromedriver._get_cache_directory = lambda: self.temp_dir.name | ||
def tearDown(self): | ||
chromedriver._get_cache_directory = self._original_get_cache_directory | ||
self.temp_dir.cleanup() |