-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
203 lines (170 loc) · 6.12 KB
/
conftest.py
File metadata and controls
203 lines (170 loc) · 6.12 KB
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
import base64
from pathlib import Path
import pytest
import allure
from slugify import slugify
try:
import pytest_html
except Exception:
pytest_html = None
# Only register custom fixtures for k11-platform tests, not globally.
import sys
import os
def _mkdir_reports():
for p in ("reports/screenshots", "reports/traces", "reports/videos"):
Path(p).mkdir(parents=True, exist_ok=True)
def _getopt(config, name: str, default=None):
"""
Robust option reader for k11 custom options only.
"""
try:
val = config.getoption(f"--k11{name}")
return default if val is None else val
except Exception:
return default
def _store_screenshot_metadata(item, screenshot_path: str, test_name: str):
with open(screenshot_path, "rb") as f:
encoded = base64.b64encode(f.read()).decode("utf-8")
item.screenshot_info = {"path": screenshot_path, "name": test_name, "base64": encoded}
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
"""
Capture test result + attach screenshot image to pytest-html report (if installed).
"""
outcome = yield
report = outcome.get_result()
setattr(item, f"rep_{report.when}", report)
if report.when == "call" and pytest_html is not None:
info = getattr(item, "screenshot_info", None)
if info:
extras = list(getattr(report, "extras", []))
extras.append(
pytest_html.extras.image(
info["base64"],
name=f"{info['name']}_screenshot",
mime_type="image/png",
extension="png",
)
)
report.extras = extras
@pytest.fixture()
def k11_artifacts(request, page, context):
"""
Autouse fixture for k11-platform tests only.
"""
test_path = str(request.node.fspath)
# Only apply for k11-platform tests
if "tests/k11-platform" not in test_path:
yield
return
_mkdir_reports()
screenshot_mode = _getopt(request.config, "screenshot", "off") # off|on|only-on-failure
tracing_mode = _getopt(request.config, "tracing", "off") # off|on|retain-on-failure
video_mode = _getopt(request.config, "video", "off") # off|on|retain-on-failure
# Start tracing (we manage tracing ourselves so we can attach to Allure reliably)
tracing_started = False
if tracing_mode in ("on", "retain-on-failure"):
context.tracing.start(screenshots=True, snapshots=True, sources=True)
tracing_started = True
yield # run the test
# Determine failure
failed = hasattr(request.node, "rep_call") and request.node.rep_call.failed
# ---------- TRACE ----------
if tracing_started:
from slugify import slugify
trace_name = slugify(request.node.nodeid)[:150]
trace_path = Path(f"reports/traces/{trace_name}.zip")
if tracing_mode == "on" or (tracing_mode == "retain-on-failure" and failed):
context.tracing.stop(path=str(trace_path))
allure.attach.file(
str(trace_path),
name=f"{request.node.name}_trace",
attachment_type=allure.attachment_type.ZIP,
)
else:
# stop without saving
context.tracing.stop()
# ---------- SCREENSHOT ----------
take_screenshot = (screenshot_mode == "on") or (
screenshot_mode == "only-on-failure" and failed
)
if take_screenshot:
from slugify import slugify
shot_name = slugify(request.node.nodeid)[:150]
shot_path = Path(f"reports/screenshots/{shot_name}.png")
page.screenshot(path=str(shot_path), full_page=True)
_store_screenshot_metadata(request.node, str(shot_path), request.node.name)
allure.attach.file(
str(shot_path),
name=f"{request.node.name}_screenshot",
attachment_type=allure.attachment_type.PNG,
)
# ---------- VIDEO ----------
if video_mode in ("on", "retain-on-failure"):
# ensure video is finalized (saved on context close)
try:
context.close()
except Exception:
pass
if video_mode == "on" or (video_mode == "retain-on-failure" and failed):
try:
if page.video:
vpath = Path(page.video.path())
if vpath.exists():
allure.attach.file(
str(vpath),
name=f"{request.node.name}_video",
attachment_type=allure.attachment_type.WEBM,
)
except Exception:
pass
# Register custom command-line options for Playwright sync fixtures
def pytest_addoption(parser):
parser.addoption(
"--k11browser",
action="store",
default="chromium",
help="Browser to use for Playwright tests (chromium, firefox, webkit)"
)
parser.addoption(
"--k11headed",
action="store_true",
default=False,
help="Run browser in headed mode (default: headless)"
)
parser.addoption(
"--k11video",
action="store",
default="off",
help="Video recording: on, retain-on-failure, off"
)
parser.addoption(
"--k11screenshot",
action="store",
default="off",
help="Screenshot capture: on, only-on-failure, off"
)
parser.addoption(
"--k11tracing",
action="store",
default="off",
help="Tracing: on, retain-on-failure, off"
)
parser.addoption(
"--k11device",
action="store",
default=None,
help='Playwright device to emulate (e.g., "iPhone 12", "Pixel 5")'
)
@pytest.fixture(scope="session")
def browser_context_args(browser_context_args, playwright, pytestconfig):
args = dict(browser_context_args)
# device emulation (optional)
dev = pytestconfig.getoption("--k11device")
if dev:
args.update(playwright.devices[dev])
# video output dir (optional)
video_mode = pytestconfig.getoption("--k11video")
if video_mode in ("on", "retain-on-failure"):
args["record_video_dir"] = "reports/videos"
return args