Skip to content

Commit cbf98cd

Browse files
committed
[py]: lint conftest.py
1 parent d20db99 commit cbf98cd

File tree

2 files changed

+74
-64
lines changed

2 files changed

+74
-64
lines changed

py/conftest.py

Lines changed: 70 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -20,63 +20,65 @@
2020
import socket
2121
import subprocess
2222
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
2326

2427
import pytest
2528

2629
from selenium import webdriver
2730
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
3231

3332
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",
4342
)
4443

4544

4645
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")
5860

5961

6062
def pytest_ignore_collect(path, config):
61-
drivers_opt = config.getoption('drivers')
63+
drivers_opt = config.getoption("drivers")
6264
_drivers = set(drivers).difference(drivers_opt or drivers)
6365
if drivers_opt:
64-
_drivers.add('unit')
66+
_drivers.add("unit")
6567
parts = path.dirname.split(os.path.sep)
6668
return len([d for d in _drivers if d.lower() in parts]) > 0
6769

6870

6971
driver_instance = None
7072

7173

72-
@pytest.fixture(scope='function')
74+
@pytest.fixture(scope="function")
7375
def driver(request):
7476
kwargs = {}
7577

7678
try:
7779
driver_class = request.param.capitalize()
7880
except AttributeError:
79-
raise Exception('This test requires a --driver to be specified.')
81+
raise Exception("This test requires a --driver to be specified.")
8082

8183
# skip tests if not available on the platform
8284
_platform = platform.system()
@@ -88,7 +90,7 @@ def driver(request):
8890
pytest.skip("Webkit tests can only run on Linux")
8991

9092
# 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()}")
9294

9395
if marker is not None:
9496
if "run" in marker.kwargs:
@@ -105,31 +107,32 @@ def fin():
105107
if driver_instance is not None:
106108
driver_instance.quit()
107109
driver_instance = None
110+
108111
request.addfinalizer(fin)
109112

110113
driver_path = request.config.option.executable
111114
options = None
112115

113116
global driver_instance
114117
if driver_instance is None:
115-
if driver_class == 'Firefox':
118+
if driver_class == "Firefox":
116119
options = get_options(driver_class, request.config)
117-
if driver_class == 'Chrome':
120+
if driver_class == "Chrome":
118121
options = get_options(driver_class, request.config)
119-
if driver_class == 'Remote':
122+
if driver_class == "Remote":
120123
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":
124127
options = get_options(driver_class, request.config)
125-
if driver_class == 'Edge':
128+
if driver_class == "Edge":
126129
options = get_options(driver_class, request.config)
127-
if driver_class == 'WPEWebKit':
130+
if driver_class == "WPEWebKit":
128131
options = get_options(driver_class, request.config)
129132
if driver_path is not None:
130-
kwargs['executable_path'] = driver_path
133+
kwargs["executable_path"] = driver_path
131134
if options is not None:
132-
kwargs['options'] = options
135+
kwargs["options"] = options
133136

134137
driver_instance = getattr(webdriver, driver_class)(**kwargs)
135138
yield driver_instance
@@ -144,13 +147,13 @@ def get_options(driver_class, config):
144147
headless = bool(config.option.headless)
145148
options = None
146149

147-
if driver_class == 'ChromiumEdge':
148-
options = getattr(webdriver, 'EdgeOptions')()
150+
if driver_class == "ChromiumEdge":
151+
options = getattr(webdriver, "EdgeOptions")()
149152

150153
if browser_path or browser_args:
151154
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":
154157
options.overlay_scrollbars_enabled = False
155158
if browser_path is not None:
156159
options.binary_location = browser_path
@@ -166,13 +169,14 @@ def get_options(driver_class, config):
166169
return options
167170

168171

169-
@pytest.fixture(scope='session', autouse=True)
172+
@pytest.fixture(scope="session", autouse=True)
170173
def stop_driver(request):
171174
def fin():
172175
global driver_instance
173176
if driver_instance is not None:
174177
driver_instance.quit()
175178
driver_instance = None
179+
176180
request.addfinalizer(fin)
177181

178182

@@ -192,20 +196,23 @@ def url(self, name, localhost=False):
192196

193197
def load(self, name):
194198
driver.get(self.url(name))
199+
195200
return Pages()
196201

197202

198-
@pytest.fixture(autouse=True, scope='session')
203+
@pytest.fixture(autouse=True, scope="session")
199204
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:
202207
yield None
203208
return
204209

205-
_host = 'localhost'
210+
_host = "localhost"
206211
_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+
)
209216

210217
def wait_for_server(url, timeout):
211218
start = time.time()
@@ -218,24 +225,26 @@ def wait_for_server(url, timeout):
218225
return 0
219226

220227
_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
221-
url = f'http://{_host}:{_port}/status'
228+
url = f"http://{_host}:{_port}/status"
222229
try:
223230
_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+
)
226235
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")
232241
yield process
233242
process.terminate()
234243
process.wait()
235-
print('Selenium server has been terminated')
244+
print("Selenium server has been terminated")
236245

237246

238-
@pytest.fixture(autouse=True, scope='session')
247+
@pytest.fixture(autouse=True, scope="session")
239248
def webserver():
240249
webserver = SimpleWebServer(host=get_lan_ip())
241250
webserver.start()
@@ -246,4 +255,5 @@ def webserver():
246255
@pytest.fixture
247256
def edge_service():
248257
from selenium.webdriver.edge.service import Service as EdgeService
258+
249259
return EdgeService

py/tox.ini

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ deps =
3838
flake8-typing-imports==1.13.0
3939
commands =
4040
; execute isort in check only mode.
41-
isort --check-only --diff selenium/ test/
41+
isort --check-only --diff selenium/ test/ conftest.py
4242
; execute black in check only mode with diff.
43-
black --check --diff selenium/ test/ -l 120
43+
black --check --diff selenium/ test/ conftest.py -l 120
4444
flake8 selenium/ test/ --min-python-version=3.7
4545

4646
[testenv:linting]
@@ -54,6 +54,6 @@ deps =
5454
flake8==5.0.4
5555
flake8-typing-imports==1.13.0
5656
commands =
57-
isort selenium/ test/
58-
black selenium/ test/ -l 120
57+
isort selenium/ test/ conftest.py
58+
black selenium/ test/ conftest.py -l 120
5959
flake8 selenium/ test/ --min-python-version=3.7

0 commit comments

Comments
 (0)