Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions py/selenium/webdriver/common/selenium_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def driver_location(self, options: BaseOptions) -> str:

browser = options.capabilities["browserName"]

args = [str(self.get_binary()), "--browser", browser, "--output", "json"]
args = [str(self.get_binary()), "--browser", browser]

if options.browser_version:
args.append("--browser-version")
Expand All @@ -87,30 +87,41 @@ def driver_location(self, options: BaseOptions) -> str:
value = proxy.ssl_proxy if proxy.ssl_proxy else proxy.http_proxy
args.append(value)

if logger.getEffectiveLevel() == logging.DEBUG:
args.append("--debug")
output = self.run(args)

browser_path = output["browser_path"]
driver_path = output["driver_path"]
logger.debug(f"Using driver at: {driver_path}")

try:
options.binary_location = browser_path
options.browser_version = None # geckodriver complains if this dev / nightly, etc
except AttributeError:
pass # do not set on options classes that do not support it

result = self.run(args)
executable = result.split("\t")[-1].strip()
logger.debug(f"Using driver at: {executable}")
return executable
return driver_path

@staticmethod
def run(args: List[str]) -> str:
def run(args: List[str]) -> dict:
"""
Executes the Selenium Manager Binary.
:Args:
- args: the components of the command being executed.
:Returns: The log string containing the driver location.
"""
if logger.getEffectiveLevel() == logging.DEBUG:
args.append("--debug")
args.append("--output")
args.append("json")

command = " ".join(args)
logger.debug(f"Executing process: {command}")
try:
completed_proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout = completed_proc.stdout.decode("utf-8").rstrip("\n")
stderr = completed_proc.stderr.decode("utf-8").rstrip("\n")
output = json.loads(stdout)
result = output["result"]["message"]
result = output["result"]
except Exception as err:
raise WebDriverException(f"Unsuccessful command executed: {command}; {err}")

Expand Down
19 changes: 14 additions & 5 deletions py/test/selenium/webdriver/common/selenium_manager_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ def test_browser_version_is_used_for_sm(mocker):
mock_run = mocker.patch("subprocess.run")
mocked_result = Mock()
mocked_result.configure_mock(
**{"stdout.decode.return_value": '{"result": {"message": "driver"}, "logs": []}', "returncode": 0}
**{
"stdout.decode.return_value": '{"result": {"driver_path": "driver", "browser_path": "browser"}, "logs": []}',
"returncode": 0,
}
)
mock_run.return_value = mocked_result
options = Options()
Expand All @@ -52,7 +55,10 @@ def test_browser_path_is_used_for_sm(mocker):
mock_run = mocker.patch("subprocess.run")
mocked_result = Mock()
mocked_result.configure_mock(
**{"stdout.decode.return_value": '{"result": {"message": "driver"}, "logs": []}', "returncode": 0}
**{
"stdout.decode.return_value": '{"result": {"driver_path": "driver", "browser_path": "browser"}, "logs": []}',
"returncode": 0,
}
)
mock_run.return_value = mocked_result
options = Options()
Expand All @@ -71,7 +77,10 @@ def test_proxy_is_used_for_sm(mocker):
mock_run = mocker.patch("subprocess.run")
mocked_result = Mock()
mocked_result.configure_mock(
**{"stdout.decode.return_value": '{"result": {"message": "driver"}, "logs": []}', "returncode": 0}
**{
"stdout.decode.return_value": '{"result": {"driver_path": "driver", "browser_path": "browser"}, "logs": []}',
"returncode": 0,
}
)
mock_run.return_value = mocked_result
options = Options()
Expand All @@ -87,11 +96,11 @@ def test_proxy_is_used_for_sm(mocker):


def test_stderr_is_propagated_to_exception_messages():
msg = r"Unsuccessful command executed:.* --browser foo --output json\.\nInvalid browser name: foo\n"
msg = r"Unsuccessful command executed:.*\n.* 'Invalid browser name: foo'.*"
with pytest.raises(WebDriverException, match=msg):
manager = SeleniumManager()
binary = manager.get_binary()
_ = manager.run([str(binary), "--browser", "foo", "--output", "json"])
_ = manager.run([str(binary), "--browser", "foo"])


def test_driver_finder_error(mocker):
Expand Down