SeleniumBase/examples/upgrade_chromedriver.py

100 lines
4.2 KiB
Python
Raw Normal View History

2023-02-03 13:27:36 +08:00
"""This script installs the chromedriver version that matches your Chrome.
On newer versions of Python, you may replace "testdir" with "pytester".
2023-02-03 13:27:36 +08:00
(Run with "pytest")"""
import subprocess
2022-04-14 01:40:01 +08:00
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
2022-10-02 08:29:20 +08:00
subprocess.check_call("seleniumbase get chromedriver", shell=True)
testdir = self.basic_run(testdir)
2022-07-14 04:30:47 +08:00
result = testdir.inline_run("--headless", "-s") # Upgrades as needed
try:
assert result.matchreport("test_passing").passed
except Exception:
# Install the compatibility version of chromedriver
2022-07-14 04:30:47 +08:00
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)
2022-07-14 04:30:47 +08:00
testdir.inline_run("--headless", "-s")
2023-08-06 03:38:18 +08:00
if __name__ == "__main__":
from pytest import main
main([__file__])