Skip to content

Commit

Permalink
[py] Using json output with Selenium Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
diemol committed Mar 20, 2023
1 parent bae493d commit 00a2624
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
24 changes: 18 additions & 6 deletions py/selenium/webdriver/common/selenium_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import json
import logging
import subprocess
import sys
Expand Down Expand Up @@ -70,14 +71,20 @@ def driver_location(self, browser: str) -> str:
if browser == "ie":
browser = "iexplorer"

binary, flag, browser = str(self.get_binary()), "--browser", browser
result = self.run((binary, flag, browser))
binary, browser_flag, browser, output_flag, output = (
str(self.get_binary()),
"--browser",
browser,
"--output",
"json",
)
result = self.run((binary, browser_flag, browser, output_flag, output))
executable = result.split("\t")[-1].strip()
logger.debug(f"Using driver at: {executable}")
return executable

@staticmethod
def run(args: Tuple[str, str, str]) -> str:
def run(args: Tuple[str, str, str, str, str]) -> str:
"""
Executes the Selenium Manager Binary.
:Args:
Expand All @@ -89,8 +96,13 @@ def run(args: Tuple[str, str, str]) -> str:
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"]
if completed_proc.returncode:
raise SeleniumManagerException(f"Selenium manager failed for: {command}.\n{stdout}{stderr}")
raise SeleniumManagerException(f"Selenium manager failed for: {command}.\n{result}{stderr}")
else:
# selenium manager exited 0 successfully, parse the executable path from stdout.
return stdout.split("\t")[-1].strip()
# Selenium Manager exited 0 successfully, return executable path and print warnings (if any)
for item in output["logs"]:
if item["level"] == "WARN":
logger.warning(item["message"])
return result
4 changes: 2 additions & 2 deletions py/test/selenium/webdriver/common/selenium_manager_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def test_non_supported_browser_raises_sme():


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

0 comments on commit 00a2624

Please sign in to comment.