2020import socket
2121import subprocess
2222import time
23+ from dataclasses import dataclass
2324from test .selenium .webdriver .common .network import get_lan_ip
2425from test .selenium .webdriver .common .webserver import SimpleWebServer
2526from urllib .request import urlopen
@@ -101,9 +102,46 @@ def pytest_ignore_collect(path, config):
101102selenium_driver = None
102103
103104
105+ @dataclass
106+ class SupportedDrivers :
107+ chrome : str = "Chrome"
108+ firefox : str = "Firefox"
109+ safari : str = "Safari"
110+ edge : str = "Edge"
111+ ie : str = "Ie"
112+ webkitgtk : str = "WebKitGTK"
113+ wpewebkit : str = "WPEWebkit"
114+ remote : str = "Remote"
115+
116+ def __contains__ (self , name ):
117+ if name in self .__dict__ :
118+ return True
119+ return False
120+
121+
122+ @dataclass
123+ class SupportedOptions :
124+ chrome : str = "ChromeOptions"
125+ firefox : str = "FirefoxOptions"
126+ edge : str = "EdgeOptions"
127+ safari : str = "SafariOptions"
128+ ie : str = "IeOptions"
129+ remote : str = "FirefoxOptions"
130+ webkitgtk : str = "WebKitGTKOptions"
131+ wpewebkit : str = "WPEWebkitOptions"
132+
133+ def __contains__ (self , name ):
134+ if name in self .__dict__ :
135+ return True
136+ return False
137+
138+
104139class Driver :
140+ _supported_drivers = SupportedDrivers ()
141+ _supported_options = SupportedOptions ()
142+
105143 def __init__ (self , driver_class , request ):
106- self ._driver_class = driver_class
144+ self .driver_class = driver_class
107145 self ._request = request
108146 self ._driver = None
109147 self ._platform = None
@@ -113,6 +151,16 @@ def __init__(self, driver_class, request):
113151 self .headless = driver_class
114152 self .bidi = bool (self ._request .config .option .bidi )
115153
154+ @property
155+ def driver_class (self ):
156+ return self ._driver_class
157+
158+ @driver_class .setter
159+ def driver_class (self , cls_name ):
160+ if cls_name .lower () not in self ._supported_drivers :
161+ raise AttributeError (f"Invalid driver class { cls_name .lower ()} " )
162+ self ._driver_class = getattr (self ._supported_drivers , cls_name .lower ())
163+
116164 @property
117165 def exe_platform (self ):
118166 self ._platform = platform .system ()
@@ -167,26 +215,21 @@ def options(self):
167215 return self ._options
168216
169217 @options .setter
170- def options (self , driver_class ):
171- if driver_class == "Remote" :
172- self ._options = getattr (webdriver , "FirefoxOptions" )()
218+ def options (self , cls_name ):
219+ if cls_name .lower () not in self ._supported_options :
220+ raise AttributeError (f"Invalid Options class { cls_name .lower ()} " )
221+ self ._options = getattr (webdriver , getattr (self ._supported_options , cls_name .lower ()))()
222+ if self .driver_class == self ._supported_drivers .remote :
173223 self ._options .set_capability ("moz:firefoxOptions" , {})
174224 self ._options .enable_downloads = True
175- elif driver_class .lower () == "webkitgtk" :
176- driver_class = "WebKitGTK"
177- self ._options = getattr (webdriver , f"{ driver_class } Options" )()
225+ if self .driver_class == self ._supported_drivers .webkitgtk :
178226 self ._options .overlay_scrollbars_enabled = False
179- elif driver_class .lower () == "wpewebkit" :
180- driver_class = "WPEWebKit"
181- self ._options = getattr (webdriver , f"{ driver_class } Options" )()
182- else :
183- self ._options = getattr (webdriver , f"{ driver_class } Options" )()
184227
185228 @property
186229 def service (self ):
187230 executable = self .driver_path
188231 if executable :
189- module = getattr (webdriver , self ._driver_class .lower ())
232+ module = getattr (webdriver , self .driver_class .lower ())
190233 self ._service = module .service .Service (executable_path = executable )
191234 return self ._service
192235 return None
@@ -201,7 +244,7 @@ def _initialize_driver(self):
201244 self .kwargs ["options" ] = self .options
202245 if self .driver_path is not None :
203246 self .kwargs ["service" ] = self .service
204- return getattr (webdriver , self ._driver_class )(** self .kwargs )
247+ return getattr (webdriver , self .driver_class )(** self .kwargs )
205248
206249 @property
207250 def stop_driver (self ):
0 commit comments