Skip to content

Commit 00a2624

Browse files
committed
[py] Using json output with Selenium Manager
1 parent bae493d commit 00a2624

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

py/selenium/webdriver/common/selenium_manager.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
import json
1718
import logging
1819
import subprocess
1920
import sys
@@ -70,14 +71,20 @@ def driver_location(self, browser: str) -> str:
7071
if browser == "ie":
7172
browser = "iexplorer"
7273

73-
binary, flag, browser = str(self.get_binary()), "--browser", browser
74-
result = self.run((binary, flag, browser))
74+
binary, browser_flag, browser, output_flag, output = (
75+
str(self.get_binary()),
76+
"--browser",
77+
browser,
78+
"--output",
79+
"json",
80+
)
81+
result = self.run((binary, browser_flag, browser, output_flag, output))
7582
executable = result.split("\t")[-1].strip()
7683
logger.debug(f"Using driver at: {executable}")
7784
return executable
7885

7986
@staticmethod
80-
def run(args: Tuple[str, str, str]) -> str:
87+
def run(args: Tuple[str, str, str, str, str]) -> str:
8188
"""
8289
Executes the Selenium Manager Binary.
8390
:Args:
@@ -89,8 +96,13 @@ def run(args: Tuple[str, str, str]) -> str:
8996
completed_proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
9097
stdout = completed_proc.stdout.decode("utf-8").rstrip("\n")
9198
stderr = completed_proc.stderr.decode("utf-8").rstrip("\n")
99+
output = json.loads(stdout)
100+
result = output["result"]["message"]
92101
if completed_proc.returncode:
93-
raise SeleniumManagerException(f"Selenium manager failed for: {command}.\n{stdout}{stderr}")
102+
raise SeleniumManagerException(f"Selenium manager failed for: {command}.\n{result}{stderr}")
94103
else:
95-
# selenium manager exited 0 successfully, parse the executable path from stdout.
96-
return stdout.split("\t")[-1].strip()
104+
# Selenium Manager exited 0 successfully, return executable path and print warnings (if any)
105+
for item in output["logs"]:
106+
if item["level"] == "WARN":
107+
logger.warning(item["message"])
108+
return result

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def test_non_supported_browser_raises_sme():
2828

2929

3030
def test_stderr_is_propagated_to_exception_messages():
31-
msg = r"Selenium manager failed for:.* --browser foo\.\nERROR\tInvalid browser name: foo"
31+
msg = r"Selenium manager failed for:.* --browser foo --output json\.\nInvalid browser name: foo\n"
3232
with pytest.raises(SeleniumManagerException, match=msg):
3333
manager = SeleniumManager()
3434
binary = manager.get_binary()
35-
_ = manager.run((str(binary), "--browser", "foo"))
35+
_ = manager.run((str(binary), "--browser", "foo", "--output", "json"))

0 commit comments

Comments
 (0)