24
24
from typing import List
25
25
26
26
from selenium .common import WebDriverException
27
- from selenium .webdriver .common .options import BaseOptions
28
27
29
28
logger = logging .getLogger (__name__ )
30
29
@@ -35,8 +34,26 @@ class SeleniumManager:
35
34
This implementation is still in beta, and may change.
36
35
"""
37
36
37
+ def binary_paths (self , args : List ) -> dict :
38
+ """Determines the locations of the requested assets.
39
+
40
+ :Args:
41
+ - args: the commands to send to the selenium manager binary.
42
+ :Returns: dictionary of assets and their path
43
+ """
44
+
45
+ args = [str (self ._get_binary ())] + args
46
+ if logger .getEffectiveLevel () == logging .DEBUG :
47
+ args .append ("--debug" )
48
+ args .append ("--language-binding" )
49
+ args .append ("python" )
50
+ args .append ("--output" )
51
+ args .append ("json" )
52
+
53
+ return self ._run (args )
54
+
38
55
@staticmethod
39
- def get_binary () -> Path :
56
+ def _get_binary () -> Path :
40
57
"""Determines the path of the correct Selenium Manager binary.
41
58
42
59
:Returns: The Selenium Manager executable location
@@ -45,29 +62,27 @@ def get_binary() -> Path:
45
62
"""
46
63
47
64
if (path := os .getenv ("SE_MANAGER_PATH" )) is not None :
48
- return Path (path )
49
-
50
- dirs = {
51
- ("darwin" , "any" ): "macos" ,
52
- ("win32" , "any" ): "windows" ,
53
- ("cygwin" , "any" ): "windows" ,
54
- ("linux" , "x86_64" ): "linux" ,
55
- ("freebsd" , "x86_64" ): "linux" ,
56
- ("openbsd" , "x86_64" ): "linux" ,
57
- }
58
-
59
- arch = platform .machine () if sys .platform in ("linux" , "freebsd" , "openbsd" ) else "any"
60
-
61
- directory = dirs .get ((sys .platform , arch ))
62
- if directory is None :
63
- raise WebDriverException (f"Unsupported platform/architecture combination: { sys .platform } /{ arch } " )
64
-
65
- if sys .platform in ["freebsd" , "openbsd" ]:
66
- logger .warning ("Selenium Manager binary may not be compatible with %s; verify settings" , sys .platform )
67
-
68
- file = "selenium-manager.exe" if directory == "windows" else "selenium-manager"
69
-
70
- path = Path (__file__ ).parent .joinpath (directory , file )
65
+ logger .debug ("Selenium Manager set by env SE_MANAGER_PATH to: %s" , path )
66
+ path = Path (path )
67
+ else :
68
+ allowed = {
69
+ ("darwin" , "any" ): "macos/selenium-manager" ,
70
+ ("win32" , "any" ): "windows/selenium-manager.exe" ,
71
+ ("cygwin" , "any" ): "windows/selenium-manager.exe" ,
72
+ ("linux" , "x86_64" ): "linux/selenium-manager" ,
73
+ ("freebsd" , "x86_64" ): "linux/selenium-manager" ,
74
+ ("openbsd" , "x86_64" ): "linux/selenium-manager" ,
75
+ }
76
+
77
+ arch = platform .machine () if sys .platform in ("linux" , "freebsd" , "openbsd" ) else "any"
78
+ if sys .platform in ["freebsd" , "openbsd" ]:
79
+ logger .warning ("Selenium Manager binary may not be compatible with %s; verify settings" , sys .platform )
80
+
81
+ location = allowed .get ((sys .platform , arch ))
82
+ if location is None :
83
+ raise WebDriverException (f"Unsupported platform/architecture combination: { sys .platform } /{ arch } " )
84
+
85
+ path = Path (__file__ ).parent .joinpath (location )
71
86
72
87
if not path .is_file ():
73
88
raise WebDriverException (f"Unable to obtain working Selenium Manager binary; { path } " )
@@ -76,60 +91,14 @@ def get_binary() -> Path:
76
91
77
92
return path
78
93
79
- def driver_location (self , options : BaseOptions ) -> str :
80
- """Determines the path of the correct driver.
81
-
82
- :Args:
83
- - browser: which browser to get the driver path for.
84
- :Returns: The driver path to use
85
- """
86
-
87
- browser = options .capabilities ["browserName" ]
88
-
89
- args = [str (self .get_binary ()), "--browser" , browser ]
90
-
91
- if options .browser_version :
92
- args .append ("--browser-version" )
93
- args .append (str (options .browser_version ))
94
-
95
- binary_location = getattr (options , "binary_location" , None )
96
- if binary_location :
97
- args .append ("--browser-path" )
98
- args .append (str (binary_location ))
99
-
100
- proxy = options .proxy
101
- if proxy and (proxy .http_proxy or proxy .ssl_proxy ):
102
- args .append ("--proxy" )
103
- value = proxy .ssl_proxy if proxy .ssl_proxy else proxy .http_proxy
104
- args .append (value )
105
-
106
- output = self .run (args )
107
-
108
- browser_path = output ["browser_path" ]
109
- driver_path = output ["driver_path" ]
110
- logger .debug ("Using driver at: %s" , driver_path )
111
-
112
- if hasattr (options .__class__ , "binary_location" ) and browser_path :
113
- options .binary_location = browser_path
114
- options .browser_version = None # if we have the binary location we no longer need the version
115
-
116
- return driver_path
117
-
118
94
@staticmethod
119
- def run (args : List [str ]) -> dict :
95
+ def _run (args : List [str ]) -> dict :
120
96
"""Executes the Selenium Manager Binary.
121
97
122
98
:Args:
123
99
- args: the components of the command being executed.
124
100
:Returns: The log string containing the driver location.
125
101
"""
126
- if logger .getEffectiveLevel () == logging .DEBUG :
127
- args .append ("--debug" )
128
- args .append ("--language-binding" )
129
- args .append ("python" )
130
- args .append ("--output" )
131
- args .append ("json" )
132
-
133
102
command = " " .join (args )
134
103
logger .debug ("Executing process: %s" , command )
135
104
try :
@@ -139,17 +108,22 @@ def run(args: List[str]) -> dict:
139
108
completed_proc = subprocess .run (args , capture_output = True )
140
109
stdout = completed_proc .stdout .decode ("utf-8" ).rstrip ("\n " )
141
110
stderr = completed_proc .stderr .decode ("utf-8" ).rstrip ("\n " )
142
- output = json .loads (stdout )
143
- result = output ["result" ]
111
+ output = json .loads (stdout ) if stdout != "" else {"logs" : [], "result" : {}}
144
112
except Exception as err :
145
113
raise WebDriverException (f"Unsuccessful command executed: { command } " ) from err
146
114
147
- for item in output ["logs" ]:
115
+ SeleniumManager ._process_logs (output ["logs" ])
116
+ result = output ["result" ]
117
+ if completed_proc .returncode :
118
+ raise WebDriverException (
119
+ f"Unsuccessful command executed: { command } ; code: { completed_proc .returncode } \n { result } \n { stderr } "
120
+ )
121
+ return result
122
+
123
+ @staticmethod
124
+ def _process_logs (log_items : List [dict ]):
125
+ for item in log_items :
148
126
if item ["level" ] == "WARN" :
149
127
logger .warning (item ["message" ])
150
- if item ["level" ] == "DEBUG" or item [ "level" ] == " INFO" :
128
+ elif item ["level" ] in [ "DEBUG" , " INFO"] :
151
129
logger .debug (item ["message" ])
152
-
153
- if completed_proc .returncode :
154
- raise WebDriverException (f"Unsuccessful command executed: { command } .\n { result } { stderr } " )
155
- return result
0 commit comments