-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathpytest_selenium.py
439 lines (378 loc) · 14.2 KB
/
pytest_selenium.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import argparse
import copy
from datetime import datetime
import os
import io
import logging
import pytest
from requests.structures import CaseInsensitiveDict
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.event_firing_webdriver import EventFiringWebDriver
from . import drivers
LOGGER = logging.getLogger(__name__)
SUPPORTED_DRIVERS = CaseInsensitiveDict(
{
"BrowserStack": webdriver.Remote,
"CrossBrowserTesting": webdriver.Remote,
"Chrome": webdriver.Chrome,
"Edge": webdriver.Edge,
"Firefox": webdriver.Firefox,
"IE": webdriver.Ie,
"PhantomJS": webdriver.PhantomJS,
"Remote": webdriver.Remote,
"Safari": webdriver.Safari,
"SauceLabs": webdriver.Remote,
"TestingBot": webdriver.Remote,
}
)
try:
from appium import webdriver as appiumdriver
SUPPORTED_DRIVERS["Appium"] = appiumdriver.Remote
except ImportError:
pass # Appium is optional.
def _merge(a, b):
""" merges b and a configurations.
Based on http://bit.ly/2uFUHgb
"""
for key in b:
if key in a:
if isinstance(a[key], dict) and isinstance(b[key], dict):
_merge(a[key], b[key], [] + [str(key)])
elif a[key] == b[key]:
pass # same leaf value
elif isinstance(a[key], list):
if isinstance(b[key], list):
a[key].extend(b[key])
else:
a[key].append(b[key])
else:
# b wins
a[key] = b[key]
else:
a[key] = b[key]
return a
def pytest_addhooks(pluginmanager):
from . import hooks
method = getattr(pluginmanager, "add_hookspecs", None)
if method is None:
method = pluginmanager.addhooks
method(hooks)
@pytest.fixture(scope="session")
def session_capabilities(pytestconfig):
"""Returns combined capabilities from pytest-variables and command line"""
driver = pytestconfig.getoption("driver").upper()
capabilities = getattr(DesiredCapabilities, driver, {}).copy()
if driver == "REMOTE":
browser = capabilities.get("browserName", "").upper()
capabilities.update(getattr(DesiredCapabilities, browser, {}))
capabilities.update(pytestconfig._capabilities)
return capabilities
@pytest.fixture
def capabilities(
request, driver_class, chrome_options, firefox_options, session_capabilities
):
"""Returns combined capabilities"""
capabilities = copy.deepcopy(session_capabilities) # make a copy
if driver_class == webdriver.Remote:
browser = capabilities.get("browserName", "").upper()
key, options = (None, None)
if browser == "CHROME":
key = getattr(chrome_options, "KEY", "goog:chromeOptions")
options = chrome_options.to_capabilities()
if key not in options:
key = "chromeOptions"
elif browser == "FIREFOX":
key = firefox_options.KEY
options = firefox_options.to_capabilities()
if all([key, options]):
capabilities[key] = _merge(capabilities.get(key, {}), options.get(key, {}))
capabilities.update(get_capabilities_from_markers(request.node))
return capabilities
def get_capabilities_from_markers(node):
capabilities = dict()
for level, mark in node.iter_markers_with_node("capabilities"):
LOGGER.debug(
"{0} marker <{1.name}> "
"contained kwargs <{1.kwargs}>".format(level.__class__.__name__, mark)
)
capabilities.update(mark.kwargs)
LOGGER.info("Capabilities from markers: {}".format(capabilities))
return capabilities
@pytest.fixture
def driver_args():
"""Return arguments to pass to the driver service"""
return None
@pytest.fixture
def driver_kwargs(
request,
capabilities,
chrome_options,
driver_args,
driver_class,
driver_log,
driver_path,
firefox_options,
firefox_profile,
pytestconfig,
):
kwargs = {}
driver = getattr(drivers, pytestconfig.getoption("driver").lower())
kwargs.update(
driver.driver_kwargs(
capabilities=capabilities,
chrome_options=chrome_options,
driver_args=driver_args,
driver_log=driver_log,
driver_path=driver_path,
firefox_options=firefox_options,
firefox_profile=firefox_profile,
host=pytestconfig.getoption("host"),
port=pytestconfig.getoption("port"),
service_log_path=None,
request=request,
test=".".join(split_class_and_test_names(request.node.nodeid)),
)
)
pytestconfig._driver_log = driver_log
return kwargs
@pytest.fixture(scope="session")
def driver_class(request):
driver = request.config.getoption("driver")
if driver is None:
raise pytest.UsageError("--driver must be specified")
return SUPPORTED_DRIVERS[driver]
@pytest.fixture
def driver_log(tmpdir):
"""Return path to driver log"""
return str(tmpdir.join("driver.log"))
@pytest.fixture
def driver_path(request):
return request.config.getoption("driver_path")
@pytest.fixture
def driver(request, driver_class, driver_kwargs):
"""Returns a WebDriver instance based on options and capabilities"""
driver = driver_class(**driver_kwargs)
event_listener = request.config.getoption("event_listener")
if event_listener is not None:
# Import the specified event listener and wrap the driver instance
mod_name, class_name = event_listener.rsplit(".", 1)
mod = __import__(mod_name, fromlist=[class_name])
event_listener = getattr(mod, class_name)
if not isinstance(driver, EventFiringWebDriver):
driver = EventFiringWebDriver(driver, event_listener())
request.node._driver = driver
yield driver
driver.quit()
@pytest.fixture
def selenium(driver):
yield driver
@pytest.hookimpl(trylast=True)
def pytest_configure(config):
capabilities = config._variables.get("capabilities", {})
capabilities.update({k: v for k, v in config.getoption("capabilities")})
config.addinivalue_line(
"markers",
"capabilities(kwargs): add or change existing "
"capabilities. specify capabilities as keyword arguments, for example "
"capabilities(foo="
"bar"
")",
)
if hasattr(config, "_metadata"):
config._metadata["Driver"] = config.getoption("driver")
config._metadata["Capabilities"] = capabilities
if all((config.getoption("host"), config.getoption("port"))):
config._metadata["Server"] = "{0}:{1}".format(
config.getoption("host"), config.getoption("port")
)
config._capabilities = capabilities
def pytest_report_header(config, startdir):
driver = config.getoption("driver")
if driver is not None:
return "driver: {0}".format(driver)
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
summary = []
extra = getattr(report, "extra", [])
driver = getattr(item, "_driver", None)
xfail = hasattr(report, "wasxfail")
failure = (report.skipped and xfail) or (report.failed and not xfail)
when = item.config.getini("selenium_capture_debug").lower()
capture_debug = when == "always" or (when == "failure" and failure)
if capture_debug:
exclude = item.config.getini("selenium_exclude_debug").lower()
if "logs" not in exclude:
# gather logs that do not depend on a driver instance
_gather_driver_log(item, summary, extra)
if driver is not None:
# gather debug that depends on a driver instance
if "url" not in exclude:
_gather_url(item, report, driver, summary, extra)
if "screenshot" not in exclude:
_gather_screenshot(item, report, driver, summary, extra)
if "html" not in exclude:
_gather_html(item, report, driver, summary, extra)
if "logs" not in exclude:
_gather_logs(item, report, driver, summary, extra)
# gather debug from hook implementations
item.config.hook.pytest_selenium_capture_debug(
item=item, report=report, extra=extra
)
if driver is not None:
# allow hook implementations to further modify the report
item.config.hook.pytest_selenium_runtest_makereport(
item=item, report=report, summary=summary, extra=extra
)
if summary:
report.sections.append(("pytest-selenium", "\n".join(summary)))
report.extra = extra
def _gather_url(item, report, driver, summary, extra):
try:
url = driver.current_url
except Exception as e:
summary.append("WARNING: Failed to gather URL: {0}".format(e))
return
pytest_html = item.config.pluginmanager.getplugin("html")
if pytest_html is not None:
# add url to the html report
extra.append(pytest_html.extras.url(url))
summary.append("URL: {0}".format(url))
def _gather_screenshot(item, report, driver, summary, extra):
try:
screenshot = driver.get_screenshot_as_base64()
except Exception as e:
summary.append("WARNING: Failed to gather screenshot: {0}".format(e))
return
pytest_html = item.config.pluginmanager.getplugin("html")
if pytest_html is not None:
# add screenshot to the html report
extra.append(pytest_html.extras.image(screenshot, "Screenshot"))
def _gather_html(item, report, driver, summary, extra):
try:
html = driver.page_source
except Exception as e:
summary.append("WARNING: Failed to gather HTML: {0}".format(e))
return
pytest_html = item.config.pluginmanager.getplugin("html")
if pytest_html is not None:
# add page source to the html report
extra.append(pytest_html.extras.text(html, "HTML"))
def _gather_logs(item, report, driver, summary, extra):
pytest_html = item.config.pluginmanager.getplugin("html")
try:
types = driver.log_types
except Exception as e:
# note that some drivers may not implement log types
summary.append("WARNING: Failed to gather log types: {0}".format(e))
return
for name in types:
try:
log = driver.get_log(name)
except Exception as e:
summary.append("WARNING: Failed to gather {0} log: {1}".format(name, e))
return
if pytest_html is not None:
extra.append(
pytest_html.extras.text(format_log(log), "%s Log" % name.title())
)
def _gather_driver_log(item, summary, extra):
pytest_html = item.config.pluginmanager.getplugin("html")
if hasattr(item.config, "_driver_log") and os.path.exists(item.config._driver_log):
if pytest_html is not None:
with io.open(item.config._driver_log, "r", encoding="utf8") as f:
extra.append(pytest_html.extras.text(f.read(), "Driver Log"))
summary.append("Driver log: {0}".format(item.config._driver_log))
def format_log(log):
timestamp_format = "%Y-%m-%d %H:%M:%S.%f"
entries = [
u"{0} {1[level]} - {1[message]}".format(
datetime.utcfromtimestamp(entry["timestamp"] / 1000.0).strftime(
timestamp_format
),
entry,
).rstrip()
for entry in log
]
log = "\n".join(entries)
return log
def split_class_and_test_names(nodeid):
"""Returns the class and method name from the current test"""
names = nodeid.split("::")
names[0] = names[0].replace("/", ".")
names = [x.replace(".py", "") for x in names if x != "()"]
classnames = names[:-1]
classname = ".".join(classnames)
name = names[-1]
return classname, name
class DriverAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, values)
driver = getattr(drivers, values.lower())
# set the default host and port if specified in the driver module
namespace.host = namespace.host or getattr(driver, "HOST", None)
namespace.port = namespace.port or getattr(driver, "PORT", None)
def pytest_addoption(parser):
_capture_choices = ("never", "failure", "always")
parser.addini(
"selenium_capture_debug",
help="when debug is captured {0}".format(_capture_choices),
default=os.getenv("SELENIUM_CAPTURE_DEBUG", "failure"),
)
parser.addini(
"selenium_exclude_debug",
help="debug to exclude from capture",
default=os.getenv("SELENIUM_EXCLUDE_DEBUG"),
)
_auth_choices = ("none", "token", "hour", "day")
parser.addini(
"saucelabs_job_auth",
help="Authorization options for the Sauce Labs job: {0}".format(_auth_choices),
default=os.getenv("SAUCELABS_JOB_AUTH", "none"),
)
group = parser.getgroup("selenium", "selenium")
group._addoption(
"--driver",
action=DriverAction,
choices=SUPPORTED_DRIVERS,
help="webdriver implementation.",
metavar="str",
)
group._addoption(
"--driver-path", metavar="path", help="path to the driver executable."
)
group._addoption(
"--capability",
action="append",
default=[],
dest="capabilities",
metavar=("key", "value"),
nargs=2,
help="additional capabilities.",
)
group._addoption(
"--event-listener",
metavar="str",
help="selenium eventlistener class, e.g. "
"package.module.EventListenerClassName.",
)
group._addoption(
"--host",
metavar="str",
help="host that the selenium server is listening on, "
"which will default to the cloud provider default "
"or localhost.",
)
group._addoption(
"--port",
type=int,
metavar="num",
help="port that the selenium server is listening on, "
"which will default to the cloud provider default "
"or localhost.",
)