Skip to content

Commit bd24090

Browse files
committed
[py] Selenium Manager get Browser Version from Options classes
#11372
1 parent c8f3f7f commit bd24090

File tree

5 files changed

+44
-17
lines changed

5 files changed

+44
-17
lines changed

common/common-web.iml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<module type="WEB_MODULE" version="4">
3+
<component name="ModuleRunConfigurationManager">
4+
<shared />
5+
</component>
36
<component name="NewModuleRootManager" inherit-compiler-output="true">
47
<exclude-output />
58
<content url="file://$MODULE_DIR$/src/web">

py/python.iml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
<configuration sdkName="" />
66
</facet>
77
</component>
8+
<component name="ModuleRunConfigurationManager">
9+
<shared />
10+
</component>
811
<component name="NewModuleRootManager" inherit-compiler-output="true">
912
<exclude-output />
1013
<content url="file://$MODULE_DIR$">
1114
<sourceFolder url="file://$MODULE_DIR$/selenium" isTestSource="false" />
1215
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
16+
<excludeFolder url="file://$MODULE_DIR$/venv" />
1317
</content>
14-
<orderEntry type="jdk" jdkName="Python 2.7" jdkType="Python SDK" />
18+
<orderEntry type="jdk" jdkName="Python 3.9 (py)" jdkType="Python SDK" />
1519
<orderEntry type="sourceFolder" forTests="false" />
1620
</component>
1721
<component name="sonarModuleSettings">

py/selenium/webdriver/common/options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def browser_version(self) -> str:
4444
"""
4545
:returns: the version of the browser if set, otherwise None.
4646
"""
47-
return self._caps["browserVersion"]
47+
return self._caps.get("browserVersion")
4848

4949
@browser_version.setter
5050
def browser_version(self, version: str) -> None:

py/selenium/webdriver/common/selenium_manager.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import subprocess
2020
import sys
2121
from pathlib import Path
22-
from typing import Tuple
22+
from typing import List
2323

2424
from selenium.common.exceptions import SeleniumManagerException
2525
from selenium.webdriver.common.options import BaseOptions
@@ -87,37 +87,36 @@ def driver_location(self, options: BaseOptions) -> str:
8787

8888
browser = allowed_browsers[browser]
8989

90-
binary, browser_flag, browser, output_flag, output = (
91-
str(self.get_binary()),
92-
"--browser",
93-
browser,
94-
"--output",
95-
"json",
96-
)
97-
result = self.run((binary, browser_flag, browser, output_flag, output))
90+
args = [str(self.get_binary()), "--browser", browser, "--output", "json"]
91+
92+
if options.browser_version:
93+
args.append("--browser-version")
94+
args.append(str(options.browser_version))
95+
96+
result = self.run(args)
9897
executable = result.split("\t")[-1].strip()
9998
logger.debug(f"Using driver at: {executable}")
10099
return executable
101100

102101
@staticmethod
103-
def run(args: Tuple[str, str, str, str, str]) -> str:
102+
def run(args: List[str]) -> str:
104103
"""
105104
Executes the Selenium Manager Binary.
106105
:Args:
107106
- args: the components of the command being executed.
108107
:Returns: The log string containing the driver location.
109108
"""
110109
command = " ".join(args)
111-
logger.debug(f"Executing: {command}")
110+
logger.info(f"Executing: {command}")
112111
completed_proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
113112
stdout = completed_proc.stdout.decode("utf-8").rstrip("\n")
114113
stderr = completed_proc.stderr.decode("utf-8").rstrip("\n")
115114
output = json.loads(stdout)
116115
result = output["result"]["message"]
117116
if completed_proc.returncode:
118-
raise SeleniumManagerException(f"Selenium manager failed for: {command}.\n{result}{stderr}")
117+
raise SeleniumManagerException(f"Selenium Manager failed for: {command}.\n{result}{stderr}")
119118
else:
120-
# Selenium Manager exited 0 successfully, return executable path and print warnings (if any)
119+
# Selenium Manager exited successfully, return executable path and print warnings
121120
for item in output["logs"]:
122121
if item["level"] == "WARN":
123122
logger.warning(item["message"])

py/test/selenium/webdriver/common/selenium_manager_tests.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18+
from unittest.mock import Mock
19+
1820
import pytest
1921

2022
from selenium.common.exceptions import SeleniumManagerException
@@ -30,9 +32,28 @@ def test_non_supported_browser_raises_sme():
3032
_ = SeleniumManager().driver_location(options)
3133

3234

35+
def test_browser_version_is_used_for_sm(mocker):
36+
import subprocess
37+
38+
mock_run = mocker.patch("subprocess.run")
39+
mocked_result = Mock()
40+
mocked_result.configure_mock(
41+
**{"stdout.decode.return_value": '{"result": {"message": "driver"}, "logs": []}', "returncode": 0}
42+
)
43+
mock_run.return_value = mocked_result
44+
options = Options()
45+
options.capabilities["browserName"] = "chrome"
46+
options.browser_version = 110
47+
48+
_ = SeleniumManager().driver_location(options)
49+
args, kwargs = subprocess.run.call_args
50+
assert "--browser-version" in args[0]
51+
assert "110" in args[0]
52+
53+
3354
def test_stderr_is_propagated_to_exception_messages():
34-
msg = r"Selenium manager failed for:.* --browser foo --output json\.\nInvalid browser name: foo\n"
55+
msg = r"Selenium Manager failed for:.* --browser foo --output json\.\nInvalid browser name: foo\n"
3556
with pytest.raises(SeleniumManagerException, match=msg):
3657
manager = SeleniumManager()
3758
binary = manager.get_binary()
38-
_ = manager.run((str(binary), "--browser", "foo", "--output", "json"))
59+
_ = manager.run([str(binary), "--browser", "foo", "--output", "json"])

0 commit comments

Comments
 (0)