20
20
import socket
21
21
import subprocess
22
22
import time
23
+ from test .selenium .webdriver .common .network import get_lan_ip
24
+ from test .selenium .webdriver .common .webserver import SimpleWebServer
25
+ from urllib .request import urlopen
23
26
24
27
import pytest
25
28
26
29
from selenium import webdriver
27
30
from selenium .webdriver import DesiredCapabilities
28
- from test .selenium .webdriver .common .webserver import SimpleWebServer
29
- from test .selenium .webdriver .common .network import get_lan_ip
30
-
31
- from urllib .request import urlopen
32
31
33
32
drivers = (
34
- ' chrome' ,
35
- ' edge' ,
36
- ' firefox' ,
37
- 'ie' ,
38
- ' remote' ,
39
- ' safari' ,
40
- ' webkitgtk' ,
41
- ' chromiumedge' ,
42
- ' wpewebkit' ,
33
+ " chrome" ,
34
+ " edge" ,
35
+ " firefox" ,
36
+ "ie" ,
37
+ " remote" ,
38
+ " safari" ,
39
+ " webkitgtk" ,
40
+ " chromiumedge" ,
41
+ " wpewebkit" ,
43
42
)
44
43
45
44
46
45
def pytest_addoption (parser ):
47
- parser .addoption ('--driver' , action = 'append' , choices = drivers , dest = 'drivers' ,
48
- metavar = 'DRIVER' ,
49
- help = 'driver to run tests against ({})' .format (', ' .join (drivers )))
50
- parser .addoption ('--browser-binary' , action = 'store' , dest = 'binary' ,
51
- help = 'location of the browser binary' )
52
- parser .addoption ('--driver-binary' , action = 'store' , dest = 'executable' ,
53
- help = 'location of the service executable binary' )
54
- parser .addoption ('--browser-args' , action = 'store' , dest = 'args' ,
55
- help = 'arguments to start the browser with' )
56
- parser .addoption ('--headless' , action = 'store' , dest = 'headless' ,
57
- help = "Allow tests to run in headless" )
46
+ parser .addoption (
47
+ "--driver" ,
48
+ action = "append" ,
49
+ choices = drivers ,
50
+ dest = "drivers" ,
51
+ metavar = "DRIVER" ,
52
+ help = "driver to run tests against ({})" .format (", " .join (drivers )),
53
+ )
54
+ parser .addoption ("--browser-binary" , action = "store" , dest = "binary" , help = "location of the browser binary" )
55
+ parser .addoption (
56
+ "--driver-binary" , action = "store" , dest = "executable" , help = "location of the service executable binary"
57
+ )
58
+ parser .addoption ("--browser-args" , action = "store" , dest = "args" , help = "arguments to start the browser with" )
59
+ parser .addoption ("--headless" , action = "store" , dest = "headless" , help = "Allow tests to run in headless" )
58
60
59
61
60
62
def pytest_ignore_collect (path , config ):
61
- drivers_opt = config .getoption (' drivers' )
63
+ drivers_opt = config .getoption (" drivers" )
62
64
_drivers = set (drivers ).difference (drivers_opt or drivers )
63
65
if drivers_opt :
64
- _drivers .add (' unit' )
66
+ _drivers .add (" unit" )
65
67
parts = path .dirname .split (os .path .sep )
66
68
return len ([d for d in _drivers if d .lower () in parts ]) > 0
67
69
68
70
69
71
driver_instance = None
70
72
71
73
72
- @pytest .fixture (scope = ' function' )
74
+ @pytest .fixture (scope = " function" )
73
75
def driver (request ):
74
76
kwargs = {}
75
77
76
78
try :
77
79
driver_class = request .param .capitalize ()
78
80
except AttributeError :
79
- raise Exception (' This test requires a --driver to be specified.' )
81
+ raise Exception (" This test requires a --driver to be specified." )
80
82
81
83
# skip tests if not available on the platform
82
84
_platform = platform .system ()
@@ -88,7 +90,7 @@ def driver(request):
88
90
pytest .skip ("Webkit tests can only run on Linux" )
89
91
90
92
# conditionally mark tests as expected to fail based on driver
91
- marker = request .node .get_closest_marker (f' xfail_{ driver_class .lower ()} ' )
93
+ marker = request .node .get_closest_marker (f" xfail_{ driver_class .lower ()} " )
92
94
93
95
if marker is not None :
94
96
if "run" in marker .kwargs :
@@ -105,31 +107,32 @@ def fin():
105
107
if driver_instance is not None :
106
108
driver_instance .quit ()
107
109
driver_instance = None
110
+
108
111
request .addfinalizer (fin )
109
112
110
113
driver_path = request .config .option .executable
111
114
options = None
112
115
113
116
global driver_instance
114
117
if driver_instance is None :
115
- if driver_class == ' Firefox' :
118
+ if driver_class == " Firefox" :
116
119
options = get_options (driver_class , request .config )
117
- if driver_class == ' Chrome' :
120
+ if driver_class == " Chrome" :
118
121
options = get_options (driver_class , request .config )
119
- if driver_class == ' Remote' :
122
+ if driver_class == " Remote" :
120
123
capabilities = DesiredCapabilities .FIREFOX .copy ()
121
- kwargs .update ({' desired_capabilities' : capabilities })
122
- options = get_options (' Firefox' , request .config )
123
- if driver_class == ' WebKitGTK' :
124
+ kwargs .update ({" desired_capabilities" : capabilities })
125
+ options = get_options (" Firefox" , request .config )
126
+ if driver_class == " WebKitGTK" :
124
127
options = get_options (driver_class , request .config )
125
- if driver_class == ' Edge' :
128
+ if driver_class == " Edge" :
126
129
options = get_options (driver_class , request .config )
127
- if driver_class == ' WPEWebKit' :
130
+ if driver_class == " WPEWebKit" :
128
131
options = get_options (driver_class , request .config )
129
132
if driver_path is not None :
130
- kwargs [' executable_path' ] = driver_path
133
+ kwargs [" executable_path" ] = driver_path
131
134
if options is not None :
132
- kwargs [' options' ] = options
135
+ kwargs [" options" ] = options
133
136
134
137
driver_instance = getattr (webdriver , driver_class )(** kwargs )
135
138
yield driver_instance
@@ -144,13 +147,13 @@ def get_options(driver_class, config):
144
147
headless = bool (config .option .headless )
145
148
options = None
146
149
147
- if driver_class == ' ChromiumEdge' :
148
- options = getattr (webdriver , ' EdgeOptions' )()
150
+ if driver_class == " ChromiumEdge" :
151
+ options = getattr (webdriver , " EdgeOptions" )()
149
152
150
153
if browser_path or browser_args :
151
154
if not options :
152
- options = getattr (webdriver , f' { driver_class } Options' )()
153
- if driver_class == ' WebKitGTK' :
155
+ options = getattr (webdriver , f" { driver_class } Options" )()
156
+ if driver_class == " WebKitGTK" :
154
157
options .overlay_scrollbars_enabled = False
155
158
if browser_path is not None :
156
159
options .binary_location = browser_path
@@ -166,13 +169,14 @@ def get_options(driver_class, config):
166
169
return options
167
170
168
171
169
- @pytest .fixture (scope = ' session' , autouse = True )
172
+ @pytest .fixture (scope = " session" , autouse = True )
170
173
def stop_driver (request ):
171
174
def fin ():
172
175
global driver_instance
173
176
if driver_instance is not None :
174
177
driver_instance .quit ()
175
178
driver_instance = None
179
+
176
180
request .addfinalizer (fin )
177
181
178
182
@@ -192,20 +196,23 @@ def url(self, name, localhost=False):
192
196
193
197
def load (self , name ):
194
198
driver .get (self .url (name ))
199
+
195
200
return Pages ()
196
201
197
202
198
- @pytest .fixture (autouse = True , scope = ' session' )
203
+ @pytest .fixture (autouse = True , scope = " session" )
199
204
def server (request ):
200
- drivers = request .config .getoption (' drivers' )
201
- if drivers is None or ' remote' not in drivers :
205
+ drivers = request .config .getoption (" drivers" )
206
+ if drivers is None or " remote" not in drivers :
202
207
yield None
203
208
return
204
209
205
- _host = ' localhost'
210
+ _host = " localhost"
206
211
_port = 4444
207
- _path = os .path .join (os .path .dirname (os .path .dirname (os .path .abspath (__file__ ))),
208
- 'java/src/org/openqa/selenium/grid/selenium_server_deploy.jar' )
212
+ _path = os .path .join (
213
+ os .path .dirname (os .path .dirname (os .path .abspath (__file__ ))),
214
+ "java/src/org/openqa/selenium/grid/selenium_server_deploy.jar" ,
215
+ )
209
216
210
217
def wait_for_server (url , timeout ):
211
218
start = time .time ()
@@ -218,24 +225,26 @@ def wait_for_server(url, timeout):
218
225
return 0
219
226
220
227
_socket = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
221
- url = f' http://{ _host } :{ _port } /status'
228
+ url = f" http://{ _host } :{ _port } /status"
222
229
try :
223
230
_socket .connect ((_host , _port ))
224
- print ('The remote driver server is already running or something else'
225
- 'is using port {}, continuing...' .format (_port ))
231
+ print (
232
+ "The remote driver server is already running or something else"
233
+ "is using port {}, continuing..." .format (_port )
234
+ )
226
235
except Exception :
227
- print (' Starting the Selenium server' )
228
- process = subprocess .Popen ([' java' , ' -jar' , _path , ' standalone' , ' --port' , ' 4444' ])
229
- print (f' Selenium server running as process: { process .pid } ' )
230
- assert wait_for_server (url , 10 ), f' Timed out waiting for Selenium server at { url } '
231
- print (' Selenium server is ready' )
236
+ print (" Starting the Selenium server" )
237
+ process = subprocess .Popen ([" java" , " -jar" , _path , " standalone" , " --port" , " 4444" ])
238
+ print (f" Selenium server running as process: { process .pid } " )
239
+ assert wait_for_server (url , 10 ), f" Timed out waiting for Selenium server at { url } "
240
+ print (" Selenium server is ready" )
232
241
yield process
233
242
process .terminate ()
234
243
process .wait ()
235
- print (' Selenium server has been terminated' )
244
+ print (" Selenium server has been terminated" )
236
245
237
246
238
- @pytest .fixture (autouse = True , scope = ' session' )
247
+ @pytest .fixture (autouse = True , scope = " session" )
239
248
def webserver ():
240
249
webserver = SimpleWebServer (host = get_lan_ip ())
241
250
webserver .start ()
@@ -246,4 +255,5 @@ def webserver():
246
255
@pytest .fixture
247
256
def edge_service ():
248
257
from selenium .webdriver .edge .service import Service as EdgeService
258
+
249
259
return EdgeService
0 commit comments