diff --git a/tools/wpt/browser.py b/tools/wpt/browser.py index 692de7069e8831..46afdfed64b607 100644 --- a/tools/wpt/browser.py +++ b/tools/wpt/browser.py @@ -46,7 +46,7 @@ def find_webdriver(self, channel=None): return NotImplemented @abstractmethod - def version(self, binary=None): + def version(self, binary=None, webdriver_binary=None): """Retrieve the release version of the installed browser.""" return NotImplemented @@ -367,7 +367,7 @@ def install_geckodriver_nightly(self, dest): return path - def version(self, binary=None): + def version(self, binary=None, webdriver_binary=None): """Retrieve the release version of the installed browser.""" version_string = call(binary, "--version").strip() m = re.match(r"Mozilla Firefox (.*)", version_string) @@ -394,7 +394,7 @@ def find_webdriver(self, channel=None): def install_webdriver(self, dest=None, channel=None): raise NotImplementedError - def version(self, binary=None): + def version(self, binary=None, webdriver_binary=None): return None @@ -458,7 +458,7 @@ def install_webdriver(self, dest=None, channel=None): os.chmod(path, st.st_mode | stat.S_IEXEC) return path - def version(self, binary=None): + def version(self, binary=None, webdriver_binary=None): binary = binary or self.binary if uname[0] != "Windows": try: @@ -497,7 +497,7 @@ def install_webdriver(self, dest=None, channel=None): chrome = Chrome() return chrome.install_webdriver(dest, channel) - def version(self, binary): + def version(self, binary=None, webdriver_binary=None): return None class Opera(Browser): @@ -562,7 +562,7 @@ def install_webdriver(self, dest=None, channel=None): os.chmod(path, st.st_mode | stat.S_IEXEC) return path - def version(self, binary): + def version(self, binary=None, webdriver_binary=None): """Retrieve the release version of the installed browser.""" binary = binary or self.binary try: @@ -593,7 +593,7 @@ def find_webdriver(self, channel=None): def install_webdriver(self, dest=None, channel=None): raise NotImplementedError - def version(self, binary): + def version(self, binary=None, webdriver_binary=None): return None @@ -619,7 +619,7 @@ def find_webdriver(self, channel=None): def install_webdriver(self, dest=None, channel=None): raise NotImplementedError - def version(self, binary): + def version(self, binary=None, webdriver_binary=None): return None @@ -647,8 +647,24 @@ def find_webdriver(self, channel=None): def install_webdriver(self, dest=None, channel=None): raise NotImplementedError - def version(self, binary): - return None + def version(self, binary=None, webdriver_binary=None): + if webdriver_binary is None: + logger.warn("Cannot find Safari version without safaridriver") + return None + # Use `safaridriver --version` to get the version. Example output: + # "Included with Safari 12.1 (14607.1.11)" + # "Included with Safari Technology Preview (Release 67, 13607.1.9.0.1)" + # The `--version` flag was added in STP 67, so allow the call to fail. + try: + version_string = call(webdriver_binary, "--version").strip() + except subprocess.CalledProcessError: + logger.warn("Failed to call %s --version", webdriver_binary) + return None + m = re.match(r"Included with Safari (.*)", version_string) + if not m: + logger.warn("Failed to extract version from: s%", version_string) + return None + return m.group(1) class SafariWebDriver(Safari): @@ -707,7 +723,7 @@ def find_webdriver(self, channel=None): def install_webdriver(self, dest=None, channel=None): raise NotImplementedError - def version(self, binary): + def version(self, binary=None, webdriver_binary=None): """Retrieve the release version of the installed browser.""" output = call(binary, "--version") m = re.search(r"Servo ([0-9\.]+-[a-f0-9]+)?(-dirty)?$", output.strip()) @@ -737,7 +753,7 @@ def find_webdriver(self, channel=None): def install_webdriver(self, dest=None, channel=None): raise NotImplementedError - def version(self, binary): + def version(self, binary=None, webdriver_binary=None): return None @@ -759,5 +775,5 @@ def find_webdriver(self, channel=None): def install_webdriver(self, dest=None, channel=None): raise NotImplementedError - def version(self, binary): + def version(self, binary=None, webdriver_binary=None): return None diff --git a/tools/wpt/run.py b/tools/wpt/run.py index b977d8a0cd68cc..093bc013c1b3b1 100644 --- a/tools/wpt/run.py +++ b/tools/wpt/run.py @@ -509,7 +509,8 @@ def setup_wptrunner(venv, prompt=True, install_browser=False, **kwargs): venv.install_requirements(os.path.join(wptrunner_path, "requirements.txt")) - kwargs['browser_version'] = setup_cls.browser.version(kwargs.get("binary")) + kwargs['browser_version'] = setup_cls.browser.version(binary=kwargs.get("binary"), + webdriver_binary=kwargs.get("webdriver_binary")) return kwargs diff --git a/tools/wpt/tests/test_browser.py b/tools/wpt/tests/test_browser.py new file mode 100644 index 00000000000000..4ce8606949c2e9 --- /dev/null +++ b/tools/wpt/tests/test_browser.py @@ -0,0 +1,32 @@ +import mock +import subprocess + +from tools.wpt import browser + +@mock.patch('subprocess.check_output') +def test_safari_version(mocked_check_output): + safari = browser.Safari() + + # Safari + mocked_check_output.return_value = 'Included with Safari 12.1 (14607.1.11)' + assert safari.version(webdriver_binary="safaridriver") == '12.1 (14607.1.11)' + + # Safari Technology Preview + mocked_check_output.return_value = 'Included with Safari Technology Preview (Release 67, 13607.1.9.0.1)' + assert safari.version(webdriver_binary="safaridriver") == 'Technology Preview (Release 67, 13607.1.9.0.1)' + +@mock.patch('subprocess.check_output') +def test_safari_version_errors(mocked_check_output): + safari = browser.Safari() + + # No webdriver_binary + assert safari.version() is None + + # `safaridriver --version` return gibberish + mocked_check_output.return_value = 'gibberish' + assert safari.version(webdriver_binary="safaridriver") is None + + # `safaridriver --version` fails (as it does for Safari <=12.0) + mocked_check_output.return_value = 'dummy' + mocked_check_output.side_effect = subprocess.CalledProcessError(1, 'cmd') + assert safari.version(webdriver_binary="safaridriver") is None diff --git a/tools/wpt/tests/test_run.py b/tools/wpt/tests/test_run.py index ac860e3f8c0b14..94b1202736f5de 100644 --- a/tools/wpt/tests/test_run.py +++ b/tools/wpt/tests/test_run.py @@ -57,9 +57,9 @@ def test_setup_wptrunner(venv, product): parser = run.create_parser() kwargs = vars(parser.parse_args(["--channel=nightly", product])) kwargs["prompt"] = False - kwargs["webdriver_binary"] = "fake" # Hack to get a real existing path kwargs["binary"] = sys.argv[0] + kwargs["webdriver_binary"] = sys.argv[0] if kwargs["product"] == "sauce": kwargs["product"] = "sauce:firefox:63" run.setup_wptrunner(venv, **kwargs)