Path: blob/trunk/py/test/unit/selenium/webdriver/common/selenium_manager_tests.py
4035 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617import json18import sys19from pathlib import Path20from unittest import mock2122import pytest2324import selenium25from selenium.common.exceptions import WebDriverException26from selenium.webdriver.common.selenium_manager import SeleniumManager272829def test_gets_results(monkeypatch):30expected_output = {"driver_path": "/path/to/driver"}31lib_path = "selenium.webdriver.common.selenium_manager.SeleniumManager"3233with (34mock.patch(lib_path + "._get_binary", return_value="/path/to/sm") as mock_get_binary,35mock.patch(lib_path + "._run", return_value=expected_output) as mock_run,36):37SeleniumManager().binary_paths([])3839mock_get_binary.assert_called_once()40expected_run_args = ["/path/to/sm", "--language-binding", "python", "--output", "json"]41mock_run.assert_called_once_with(expected_run_args)424344def test_uses_environment_variable(monkeypatch):45sm_path = r"\path\to\manager" if sys.platform.startswith("win") else "path/to/manager"46monkeypatch.setenv("SE_MANAGER_PATH", sm_path)47monkeypatch.setattr(Path, "is_file", lambda _: True)4849binary = SeleniumManager()._get_binary()5051assert str(binary) == sm_path525354def test_uses_windows(monkeypatch):55monkeypatch.setattr(sys, "platform", "win32")56binary = SeleniumManager()._get_binary()5758project_root = Path(selenium.__file__).parent.parent59assert binary == project_root.joinpath("selenium/webdriver/common/windows/selenium-manager.exe")606162def test_uses_linux(monkeypatch):63monkeypatch.setattr(sys, "platform", "linux")64monkeypatch.setattr("platform.machine", lambda: "x86_64")6566binary = SeleniumManager()._get_binary()67project_root = Path(selenium.__file__).parent.parent68assert binary == project_root.joinpath("selenium/webdriver/common/linux/selenium-manager")697071def test_uses_linux_arm64(monkeypatch):72monkeypatch.setattr(sys, "platform", "linux")73monkeypatch.setattr("platform.machine", lambda: "arm64")7475with pytest.raises(WebDriverException, match="Unsupported platform/architecture combination: linux/arm64"):76SeleniumManager()._get_binary()777879def test_uses_mac(monkeypatch):80monkeypatch.setattr(sys, "platform", "darwin")81binary = SeleniumManager()._get_binary()8283project_root = Path(selenium.__file__).parent.parent84assert binary == project_root.joinpath("selenium/webdriver/common/macos/selenium-manager")858687def test_errors_if_not_file(monkeypatch):88monkeypatch.setattr(Path, "is_file", lambda _: False)8990with pytest.raises(WebDriverException) as excinfo:91SeleniumManager()._get_binary()92assert "Unable to obtain working Selenium Manager binary" in str(excinfo.value)939495def test_errors_if_invalid_os(monkeypatch):96monkeypatch.setattr(sys, "platform", "linux")97monkeypatch.setattr("platform.machine", lambda: "invalid")9899with pytest.raises(WebDriverException) as excinfo:100SeleniumManager()._get_binary()101assert "Unsupported platform/architecture combination" in str(excinfo.value)102103104def test_error_if_invalid_env_path(monkeypatch):105sm_path = r"\path\to\manager" if sys.platform.startswith("win") else "path/to/manager"106monkeypatch.setenv("SE_MANAGER_PATH", sm_path)107108with pytest.raises(WebDriverException) as excinfo:109SeleniumManager()._get_binary()110assert f"SE_MANAGER_PATH does not point to a file: {sm_path}" in str(excinfo.value)111112113def test_run_successful():114expected_result = {"driver_path": "/path/to/driver", "browser_path": "/path/to/browser"}115run_output = {"result": expected_result, "logs": []}116with mock.patch("subprocess.run") as mock_run, mock.patch("json.loads", return_value=run_output):117mock_run.return_value = mock.Mock(stdout=json.dumps(run_output).encode("utf-8"), stderr=b"", returncode=0)118result = SeleniumManager._run(["arg1", "arg2"])119assert result == expected_result120121122def test_run_exception():123with mock.patch("subprocess.run", side_effect=Exception("Test Error")):124with pytest.raises(WebDriverException) as excinfo:125SeleniumManager._run(["/path/to/sm", "arg1", "arg2"])126assert "Unsuccessful command executed: /path/to/sm arg1 arg2" in str(excinfo.value)127128129def test_run_non_zero_exit_code():130with mock.patch("subprocess.run") as mock_run, mock.patch("json.loads", return_value={"result": "", "logs": []}):131mock_run.return_value = mock.Mock(stdout=b"{}", stderr=b"Error Message", returncode=1)132with pytest.raises(WebDriverException) as excinfo:133SeleniumManager._run(["/path/to/sm", "arg1"])134assert "Unsuccessful command executed: /path/to/sm arg1; code: 1\n\nError Message" in str(excinfo.value)135136137