-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathupgrade_chromedriver.py
99 lines (92 loc) · 4.16 KB
/
upgrade_chromedriver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""This script installs the chromedriver version that matches your Chrome.
On newer versions of Python, you may replace "testdir" with "pytester".
(Run with "pytest")"""
import subprocess
class TestUpgradeChromedriver:
def basic_run(self, testdir):
testdir.makepyfile(
"""
from seleniumbase import BaseCase
class MyTestCase(BaseCase):
def test_passing(self):
pass
"""
)
return testdir
def upgrade_chromedriver(self, testdir):
testdir.makepyfile(
"""
import subprocess
from seleniumbase import BaseCase
class MyTestCase(BaseCase):
def test_upgrade(self):
chrome_version = self.get_chrome_version()
major_chrome_ver = chrome_version.split(".")[0]
chromedriver_ver = self.get_chromedriver_version()
major_chromedriver_ver = chromedriver_ver.split(".")[0]
if major_chromedriver_ver != major_chrome_ver:
subprocess.check_call(
"sbase get chromedriver %s" % major_chrome_ver,
shell=True
)
"""
)
return testdir
def print_versions_of_chromedriver_and_chrome(self, testdir):
testdir.makepyfile(
"""
from seleniumbase import BaseCase
class MyTestCase(BaseCase):
def test_print_versions(self):
chrome_version = self.get_chrome_version()
major_chrome_ver = chrome_version.split(".")[0]
chromedriver_ver = self.get_chromedriver_version()
major_chromedriver_ver = chromedriver_ver.split(".")[0]
print(
"\\n* Now using chromedriver %s with Chrome %s"
% (chromedriver_ver, chrome_version)
)
if major_chromedriver_ver == major_chrome_ver:
print(
"* SUCCESS: "
"The chromedriver version is compatible "
"with Chrome!"
)
elif major_chromedriver_ver < major_chrome_ver:
print("* !!! Version Mismatch !!!")
print(
"* The version of chromedriver is too low!\\n"
"* Try upgrading to chromedriver %s manually:\\n"
"* >>> sbase get chromedriver %s <<<"
% (major_chrome_ver, major_chrome_ver)
)
else:
print("* !!! Version Mismatch !!!")
print(
"* The version of chromedriver is too high!\\n"
"* Try downgrading to chromedriver %s manually:\\n"
"* >>> sbase get chromedriver %s <<<"
% (major_chrome_ver, major_chrome_ver)
)
"""
)
return testdir
def test_upgrade_chromedriver(self, testdir):
# Find out if the installed chromedriver version works with Chrome
subprocess.check_call("seleniumbase get chromedriver", shell=True)
testdir = self.basic_run(testdir)
result = testdir.inline_run("--headless", "-s") # Upgrades as needed
try:
assert result.matchreport("test_passing").passed
except Exception:
# Install the compatibility version of chromedriver
install_command = "seleniumbase get chromedriver 72.0.3626.69"
subprocess.check_call(install_command, shell=True)
testdir = self.upgrade_chromedriver(testdir)
testdir.inline_run("--headless", "-s")
# Print the final installed versions of chromedriver and Chrome
testdir = self.print_versions_of_chromedriver_and_chrome(testdir)
testdir.inline_run("--headless", "-s")
if __name__ == "__main__":
from pytest import main
main([__file__])