Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/unit/selenium/webdriver/common/selenium_manager_tests.py
4035 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
18
import json
19
import sys
20
from pathlib import Path
21
from unittest import mock
22
23
import pytest
24
25
import selenium
26
from selenium.common.exceptions import WebDriverException
27
from selenium.webdriver.common.selenium_manager import SeleniumManager
28
29
30
def test_gets_results(monkeypatch):
31
expected_output = {"driver_path": "/path/to/driver"}
32
lib_path = "selenium.webdriver.common.selenium_manager.SeleniumManager"
33
34
with (
35
mock.patch(lib_path + "._get_binary", return_value="/path/to/sm") as mock_get_binary,
36
mock.patch(lib_path + "._run", return_value=expected_output) as mock_run,
37
):
38
SeleniumManager().binary_paths([])
39
40
mock_get_binary.assert_called_once()
41
expected_run_args = ["/path/to/sm", "--language-binding", "python", "--output", "json"]
42
mock_run.assert_called_once_with(expected_run_args)
43
44
45
def test_uses_environment_variable(monkeypatch):
46
sm_path = r"\path\to\manager" if sys.platform.startswith("win") else "path/to/manager"
47
monkeypatch.setenv("SE_MANAGER_PATH", sm_path)
48
monkeypatch.setattr(Path, "is_file", lambda _: True)
49
50
binary = SeleniumManager()._get_binary()
51
52
assert str(binary) == sm_path
53
54
55
def test_uses_windows(monkeypatch):
56
monkeypatch.setattr(sys, "platform", "win32")
57
binary = SeleniumManager()._get_binary()
58
59
project_root = Path(selenium.__file__).parent.parent
60
assert binary == project_root.joinpath("selenium/webdriver/common/windows/selenium-manager.exe")
61
62
63
def test_uses_linux(monkeypatch):
64
monkeypatch.setattr(sys, "platform", "linux")
65
monkeypatch.setattr("platform.machine", lambda: "x86_64")
66
67
binary = SeleniumManager()._get_binary()
68
project_root = Path(selenium.__file__).parent.parent
69
assert binary == project_root.joinpath("selenium/webdriver/common/linux/selenium-manager")
70
71
72
def test_uses_linux_arm64(monkeypatch):
73
monkeypatch.setattr(sys, "platform", "linux")
74
monkeypatch.setattr("platform.machine", lambda: "arm64")
75
76
with pytest.raises(WebDriverException, match="Unsupported platform/architecture combination: linux/arm64"):
77
SeleniumManager()._get_binary()
78
79
80
def test_uses_mac(monkeypatch):
81
monkeypatch.setattr(sys, "platform", "darwin")
82
binary = SeleniumManager()._get_binary()
83
84
project_root = Path(selenium.__file__).parent.parent
85
assert binary == project_root.joinpath("selenium/webdriver/common/macos/selenium-manager")
86
87
88
def test_errors_if_not_file(monkeypatch):
89
monkeypatch.setattr(Path, "is_file", lambda _: False)
90
91
with pytest.raises(WebDriverException) as excinfo:
92
SeleniumManager()._get_binary()
93
assert "Unable to obtain working Selenium Manager binary" in str(excinfo.value)
94
95
96
def test_errors_if_invalid_os(monkeypatch):
97
monkeypatch.setattr(sys, "platform", "linux")
98
monkeypatch.setattr("platform.machine", lambda: "invalid")
99
100
with pytest.raises(WebDriverException) as excinfo:
101
SeleniumManager()._get_binary()
102
assert "Unsupported platform/architecture combination" in str(excinfo.value)
103
104
105
def test_error_if_invalid_env_path(monkeypatch):
106
sm_path = r"\path\to\manager" if sys.platform.startswith("win") else "path/to/manager"
107
monkeypatch.setenv("SE_MANAGER_PATH", sm_path)
108
109
with pytest.raises(WebDriverException) as excinfo:
110
SeleniumManager()._get_binary()
111
assert f"SE_MANAGER_PATH does not point to a file: {sm_path}" in str(excinfo.value)
112
113
114
def test_run_successful():
115
expected_result = {"driver_path": "/path/to/driver", "browser_path": "/path/to/browser"}
116
run_output = {"result": expected_result, "logs": []}
117
with mock.patch("subprocess.run") as mock_run, mock.patch("json.loads", return_value=run_output):
118
mock_run.return_value = mock.Mock(stdout=json.dumps(run_output).encode("utf-8"), stderr=b"", returncode=0)
119
result = SeleniumManager._run(["arg1", "arg2"])
120
assert result == expected_result
121
122
123
def test_run_exception():
124
with mock.patch("subprocess.run", side_effect=Exception("Test Error")):
125
with pytest.raises(WebDriverException) as excinfo:
126
SeleniumManager._run(["/path/to/sm", "arg1", "arg2"])
127
assert "Unsuccessful command executed: /path/to/sm arg1 arg2" in str(excinfo.value)
128
129
130
def test_run_non_zero_exit_code():
131
with mock.patch("subprocess.run") as mock_run, mock.patch("json.loads", return_value={"result": "", "logs": []}):
132
mock_run.return_value = mock.Mock(stdout=b"{}", stderr=b"Error Message", returncode=1)
133
with pytest.raises(WebDriverException) as excinfo:
134
SeleniumManager._run(["/path/to/sm", "arg1"])
135
assert "Unsuccessful command executed: /path/to/sm arg1; code: 1\n\nError Message" in str(excinfo.value)
136
137