Skip to content

Commit 0ce0690

Browse files
Fix Windows browser version detection by using PowerShell EncodedCommand (#625)
1 parent f5a0502 commit 0ce0690

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

tests/test_utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import pytest
22
from requests import Response, Request
3+
import base64
34

45
from webdriver_manager.core.http import HttpClient
6+
from webdriver_manager.core.utils import windows_browser_apps_to_cmd
57

68

79
def test_validate_response_value_error_not_200_and_not_404():
@@ -39,3 +41,16 @@ def test_validate_response_value_error_404():
3941

4042
expected_message = "There is no such driver by url https://example.com"
4143
assert str(excinfo.value) == expected_message
44+
45+
46+
def test_windows_browser_apps_to_cmd_uses_encoded_command(monkeypatch):
47+
monkeypatch.setattr("webdriver_manager.core.utils.determine_powershell", lambda: "powershell")
48+
expression = r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Microsoft\Edge\Application\msedge.exe").VersionInfo.FileVersion'
49+
50+
cmd = windows_browser_apps_to_cmd(expression)
51+
52+
assert " -EncodedCommand " in cmd
53+
encoded = cmd.split(" -EncodedCommand ", 1)[1]
54+
script = base64.b64decode(encoded).decode("utf-16le")
55+
assert expression in script
56+
assert "$ErrorActionPreference='silentlycontinue';" in script

webdriver_manager/core/utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import re
44
import subprocess
5+
import base64
56

67

78
def get_date_diff(date1, date2, date_format):
@@ -31,8 +32,10 @@ def windows_browser_apps_to_cmd(*apps: str) -> str:
3132
script = "$ErrorActionPreference='silentlycontinue'; " + " ".join(
3233
first_hit_template.format(expression=e) for e in apps
3334
)
34-
35-
return f'{powershell} -NoProfile "{script}"'
35+
if not powershell:
36+
return script
37+
encoded_script = base64.b64encode(script.encode("utf-16le")).decode("ascii")
38+
return f"{powershell} -NoProfile -EncodedCommand {encoded_script}"
3639

3740

3841
def read_version_from_cmd(cmd, pattern):

0 commit comments

Comments
 (0)