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