-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
425 lines (353 loc) · 13.2 KB
/
conftest.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
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
import shutil
import os
import warnings
from asyncio import AbstractEventLoop
from typing import Any, Callable, Dict, Generator, List, Optional
import requests
import logging
import pytest
from pathlib import Path
from playwright.sync_api import (
Browser,
BrowserContext,
BrowserType,
Error,
Page,
Playwright,
sync_playwright,
)
from slugify import slugify
import tempfile
from filelock import FileLock
root_path = Path(__file__).parent.resolve()
artifacts_folder = tempfile.TemporaryDirectory(prefix="playwight-pytest-", dir=root_path.resolve())
# @pytest.fixture(scope="session", autouse=True)
# def delete_output_dir(tmp_path_factory, pytestconfig: Any) -> None:
# output_dir = pytestconfig.getoption("--output")
#
# with FileLock(get_lock_file(tmp_path_factory)):
# if os.path.exists(output_dir):
# shutil.rmtree(output_dir)
def get_lock_file(tmp_path_factory) -> str:
root_tmp_dir = tmp_path_factory.getbasetemp().parent
fn = root_tmp_dir / "data.json"
return str(fn) + ".lock"
def pytest_generate_tests(metafunc: Any) -> None:
if "browser_name" in metafunc.fixturenames:
browsers = metafunc.config.option.browser or ["chromium"]
metafunc.parametrize("browser_name", browsers, scope="session")
def pytest_configure(config: Any) -> None:
config.addinivalue_line(
"markers", "skip_browser(name): mark test to be skipped a specific browser"
)
config.addinivalue_line(
"markers", "only_browser(name): mark test to run only on a specific browser"
)
@pytest.mark.optionalhook
def pytest_reporter_context(context, config):
context["title"] = "PAF Test Report"
# Making test result information available in fixtures
# https://docs.pytest.org/en/latest/example/simple.html#making-test-result-information-available-in-fixtures
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item: Any) -> Generator[None, Any, None]:
# execute all other hooks to obtain the report object
outcome = yield
rep = outcome.get_result()
test_fn = item.obj
rep.extra = []
# get the test level docstrings as test long descriptions
docstring = getattr(test_fn, '__doc__')
if docstring:
rep.description = docstring
# set a report attribute for each phase of a call, which can
# be "setup", "call", "teardown"
if rep.when == 'setup':
rep.extra.append(
{
"name": "Browser Channel",
"format": "text",
"content": f"{'' if not item.funcargs['browser_channel'] else item.funcargs['browser_channel']}"
}
)
if rep.when == 'call' and rep.failed is True:
rep.extra.append(
{
"name": "Screenshot",
"format": "image",
"content": os.path.join(os.getcwd(), "reports", slugify(rep.nodeid), f"{item.name}.png"),
}
)
rep.extra.append(
{
"name": "Recording",
"format": "video",
"content": os.path.join(os.getcwd(), "reports", slugify(rep.nodeid), f"{item.name}.webm"),
},
)
setattr(item, "rep_" + rep.when, rep)
@pytest.fixture(autouse=True, scope="function")
def test_listeners(request):
yield
# request.node is an "item" because we use the default
# "function" scope
if request.node.rep_call.failed:
# TODO Testrail code for "fail" goes here
logging.info(f"executing test failed! {request.node.rep_call.longrepr.reprcrash.message}")
elif request.node.rep_call.passed:
# TODO Testrail code for "pass" goes here
logging.info(f"executing test passed {request.node.nodeid}")
elif request.node.rep_call.skipped:
# TODO Testrail code for "skipped" goes here
logging.info(f"executing test skipped {request.node.nodeid}")
def _get_skiplist(item: Any, values: List[str], value_name: str) -> List[str]:
skipped_values: List[str] = []
# Allowlist
only_marker = item.get_closest_marker(f"only_{value_name}")
if only_marker:
skipped_values = values
skipped_values.remove(only_marker.args[0])
# Denylist
skip_marker = item.get_closest_marker(f"skip_{value_name}")
if skip_marker:
skipped_values.append(skip_marker.args[0])
return skipped_values
def pytest_runtest_setup(item: Any) -> None:
if not hasattr(item, "callspec"):
return
browser_name = item.callspec.params.get("browser_name")
if not browser_name:
return
skip_browsers_names = _get_skiplist(
item, ["chromium", "firefox", "webkit"], "browser"
)
if browser_name in skip_browsers_names:
pytest.skip("skipped for this browser: {}".format(browser_name))
@pytest.fixture(scope="session")
def event_loop() -> Generator[AbstractEventLoop, None, None]:
loop = asyncio.get_event_loop()
yield loop
loop.close()
@pytest.fixture(scope="session")
def browser_type_launch_args(pytestconfig: Any, browser_name: str) -> Dict:
launch_options = {}
headed_option = pytestconfig.getoption("--headed")
if headed_option:
launch_options["headless"] = False
browser_channel_option = pytestconfig.getoption("--browser-channel")
if browser_channel_option:
launch_options["channel"] = browser_channel_option
slowmo_option = pytestconfig.getoption("--slowmo")
if slowmo_option:
launch_options["slow_mo"] = slowmo_option
return launch_options
def _build_artifact_test_folder(
pytestconfig: Any, request: pytest.FixtureRequest, folder_or_file_name: str) -> str:
output_dir = pytestconfig.getoption("--output")
return os.path.join(output_dir, slugify(request.node.nodeid), folder_or_file_name)
@pytest.fixture()
def browser_context_args(
pytestconfig: Any,
playwright: Playwright,
device: Optional[str]
) -> Dict:
context_args = {}
if device:
context_args.update(playwright.devices[device])
base_url = pytestconfig.getoption("--base-url")
if base_url:
context_args["base_url"] = base_url
video_option = pytestconfig.getoption("--video")
capture_video = video_option in ["on", "retain-on-failure"]
if capture_video:
context_args["record_video_dir"] = artifacts_folder.name
return context_args
@pytest.fixture(scope="session")
def playwright() -> Generator[Playwright, None, None]:
pw = sync_playwright().start()
yield pw
pw.stop()
@pytest.fixture(scope="session")
def browser_type(playwright: Playwright, browser_name: str) -> BrowserType:
return getattr(playwright, browser_name)
@pytest.fixture(scope="session")
def launch_browser(
browser_type_launch_args: Dict,
browser_type: BrowserType,
) -> Callable[..., Browser]:
def launch(**kwargs: Dict) -> Browser:
launch_options = {**browser_type_launch_args, **kwargs}
browser = browser_type.launch(**launch_options)
return browser
return launch
@pytest.fixture(scope="session")
def browser(launch_browser: Callable[[], Browser]) -> Generator[Browser, None, None]:
browser = launch_browser()
yield browser
browser.close()
artifacts_folder.cleanup()
@pytest.fixture
def context(
browser: Browser,
browser_context_args: Dict,
pytestconfig: Any,
request: pytest.FixtureRequest,
) -> Generator[BrowserContext, None, None]:
pages: List[Page] = []
context = browser.new_context(**browser_context_args)
context.on("page", lambda page: pages.append(page))
tracing_option = pytestconfig.getoption("--tracing")
capture_trace = tracing_option in ["on", "retain-on-failure"]
if capture_trace:
context.tracing.start(
name=slugify(request.node.nodeid),
screenshots=True,
snapshots=True,
)
yield context
if capture_trace:
retain_trace = tracing_option == "on" or (
request.node.rep_call.failed and tracing_option == "retain-on-failure"
)
if retain_trace:
trace_path = _build_artifact_test_folder(pytestconfig, request, "trace.zip")
context.tracing.stop(path=trace_path)
else:
context.tracing.stop()
screenshot_option = pytestconfig.getoption("--screenshot")
capture_screenshot = screenshot_option == "on" or (
request.node.rep_call.failed and screenshot_option == "only-on-failure"
)
if capture_screenshot:
for index, page in enumerate(pages):
human_readable_status = (
"failed" if request.node.rep_call.failed else "finished"
)
screenshot_path = _build_artifact_test_folder(
# pytestconfig, request, f"{request.node.name}-{human_readable_status}-{index+1}.png"
pytestconfig, request, f"{request.node.name}.png"
)
try:
page.screenshot(timeout=5000, path=screenshot_path)
except Error:
pass
context.close()
video_option = pytestconfig.getoption("--video")
preserve_video = video_option == "on" or (
video_option == "retain-on-failure" and request.node.rep_call.failed
)
if preserve_video:
for page in pages:
video = page.video
if not video:
continue
try:
# video_path = video.path()
# file_name = os.path.basename(video_path)
file_name = request.node.name + ".webm"
video.save_as(
path=_build_artifact_test_folder(pytestconfig, request, file_name)
)
except Error:
# Silent catch empty videos.
pass
@pytest.fixture
def page(context: BrowserContext, base_url: str) -> Generator[Page, None, None]:
page = context.new_page()
yield page
@pytest.fixture(scope="session")
def is_webkit(browser_name: str) -> bool:
return browser_name == "webkit"
@pytest.fixture(scope="session")
def is_firefox(browser_name: str) -> bool:
return browser_name == "firefox"
@pytest.fixture(scope="session")
def is_chromium(browser_name: str) -> bool:
return browser_name == "chromium"
@pytest.fixture(scope="session")
def browser_name(pytestconfig: Any) -> Optional[str]:
# When using unittest.TestCase it won't use pytest_generate_tests
# For that we still try to give the user a slightly less feature-rich experience
browser_names = pytestconfig.getoption("--browser")
if len(browser_names) == 0:
return "chromium"
if len(browser_names) == 1:
return browser_names[0]
warnings.warn(
"When using unittest.TestCase specifying multiple browsers is not supported"
)
return browser_names[0]
@pytest.fixture(scope="session", autouse=True)
def browser_channel(pytestconfig: Any) -> Optional[str]:
return pytestconfig.getoption("--browser-channel")
@pytest.fixture(scope="session")
def device(pytestconfig: Any) -> Optional[str]:
return pytestconfig.getoption("--device")
def pytest_addoption(parser: Any) -> None:
group = parser.getgroup("playwright", "Playwright")
logging.info("Preparing pytest and playwright flags")
group.addoption(
"--browser",
action="append",
default=[],
help="Browser engine which should be used",
choices=["chromium", "firefox", "webkit"],
)
group.addoption(
"--headed",
action="store_true",
default=False,
help="Run tests in headed mode.",
)
group.addoption(
"--browser-channel",
action="store",
default=None,
choices=["chrome", "chrome-beta", "msedge", "msedge-beta", "msedge-dev"],
help="Browser channel to be used.",
)
group.addoption(
"--slowmo",
default=0,
type=int,
help="Run tests with slow mo",
)
group.addoption(
"--device", default=None, action="store", help="Device to be emulated."
)
group.addoption(
"--output",
default="test-results",
help="Directory for artifacts produced by tests, defaults to test-results.",
)
group.addoption(
"--tracing",
default="off",
choices=["on", "off", "retain-on-failure"],
help="Whether to record a trace for each test.",
)
group.addoption(
"--video",
default="retain-on-failure",
choices=["on", "off", "retain-on-failure"],
help="Whether to record video for each test.",
)
group.addoption(
"--screenshot",
default="only-on-failure",
choices=["on", "off", "only-on-failure"],
help="Whether to automatically capture a screenshot after each test.",
)