-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
321 lines (234 loc) · 9.27 KB
/
Copy pathconftest.py
File metadata and controls
321 lines (234 loc) · 9.27 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
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
import sys
import os
import pytest
import time
# ---------------- PATH SETUP ----------------
ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
sys.path.insert(0, ROOT_DIR)
# ---------------- IMPORTS ----------------
from utils.driver_factory import get_driver
from utils.screenshot_utils import capture_screenshot
from utils.execution_metrics import ExecutionMetrics
from utils.data_provider import load_login_data
from utils.flaky_tracker import FlakyTracker
from utils.data_provider import load_dataset, validate_dataset_structure
from utils.config_manager import ConfigManager
from utils.run_metadata_manager import RunMetadataManager
from utils.tag_manager import TagManager
from utils.test_impact_analyzer import TestImpactAnalyzer
from utils.dependency_graph_builder import DependencyGraphBuilder
from utils.failure_root_analyzer import FailureRootAnalyzer
# ---------------- GLOBAL METRICS OBJECT ----------------
metrics = ExecutionMetrics()
flaky_tracker = FlakyTracker()
run_metadata_manager = RunMetadataManager()
tag_manager = TagManager()
impact_analyzer = TestImpactAnalyzer()
dependency_graph_builder = DependencyGraphBuilder()
failure_root_analyzer = FailureRootAnalyzer()
# ---------------- APP URL CONFIG ----------------
APP_URLS = {
"saucedemo": "https://www.saucedemo.com/",
"orangehrm": "https://opensource-demo.orangehrmlive.com/",
"the_internet": "https://the-internet.herokuapp.com/",
}
# ---------------- CLI OPTIONS ----------------
def pytest_addoption(parser):
parser.addoption(
"--app", action="store", default="saucedemo", help="Application under test"
)
parser.addoption(
"--env", action="store", default="qa", help="Environment profile (dev/qa/prod)"
)
parser.addoption(
"--headless", action="store_true", help="Run browser in headless mode"
)
parser.addoption(
"--fail-fast", action="store_true", help="Stop execution on first failure"
)
parser.addoption(
"--max-failures",
action="store",
default=None,
help="Stop execution after N failures",
)
parser.addoption(
"--ci-mode",
action="store_true",
help="Enable strict CI validation mode",
)
parser.addoption(
"--tag",
action="store",
default=None,
help="Run tests matching a specific tag",
)
parser.addoption(
"--impact",
action="store_true",
help="Run only tests impacted by recent code changes",
)
# ---------------- DRIVER FIXTURE ----------------
@pytest.fixture(params=["chrome", "firefox", "edge"])
def driver(request):
app_name = request.config.getoption("--app")
if app_name not in APP_URLS:
raise ValueError(f"Invalid app name: {app_name}")
headless = request.config.getoption("--headless")
driver = get_driver(request.param, headless=headless)
driver.get(APP_URLS[app_name])
yield driver
driver.quit()
# ---------------- TEST DURATION TRACKER ----------------
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(item):
start = time.time()
yield
duration = time.time() - start
metrics.record_test(item.name, duration)
# ---------------- PYTEST REPORT HOOK ----------------
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
rep = outcome.get_result()
setattr(item, "rep_" + rep.when, rep)
@pytest.hookimpl
def pytest_runtest_logreport(report):
if report.when == "call" and report.failed:
flaky_tracker.record_retry(report.nodeid)
failure_root_analyzer.record_failure(report.nodeid, str(report.longrepr))
# ---------------- SCREENSHOT ON FAILURE ----------------
@pytest.fixture(autouse=True)
def screenshot_on_failure(request, driver):
yield
if request.node.rep_call.failed:
capture_screenshot(driver, request.node.name)
# ---------------- MARKER REGISTRATION + REPORT METADATA ----------------
def pytest_configure(config):
config.addinivalue_line("markers", "smoke: Smoke tests")
config.addinivalue_line("markers", "regression: Regression tests")
config.addinivalue_line("markers", "sanity: Sanity tests")
if hasattr(config, "_metadata"):
config._metadata["Project"] = "Python Selenium QA Framework"
config._metadata["Execution Mode"] = "Parallel Supported"
config._metadata["Browser Param"] = "Multi-browser"
config._metadata["Tester"] = "Sahil Singh"
# ---------------- HTML REPORT TITLE ----------------
def pytest_html_report_title(report):
report.title = "Automation Execution Report"
# ---------------- PARALLEL EXECUTION INFO ----------------
@pytest.fixture(autouse=True)
def print_worker_info(request):
worker_id = (
request.config.workerinput["workerid"]
if hasattr(request.config, "workerinput")
else "master"
)
print(f"\n[Running on worker: {worker_id}]")
# ---------------- ENVIRONMENT PROFILE FIXTURE ----------------
@pytest.fixture(scope="session")
def env_config(request):
from utils.env_loader import load_environment
env_name = request.config.getoption("--env")
return load_environment(env_name)
# ---------------- DATA PROVIDER FIXTURES ----------------
@pytest.fixture(scope="session")
def valid_login_data():
return load_login_data("valid")
@pytest.fixture(scope="session")
def invalid_login_data():
return load_login_data("invalid")
@pytest.fixture(scope="session")
def login_dataset():
dataset = load_dataset("test_login_dataset")
validate_dataset_structure(dataset)
return dataset
# ---------------- SESSION FINISH (EXECUTION CONTROL + METRICS) ----------------
def pytest_sessionfinish(session, exitstatus):
config = session.config
# ----- Fail Fast -----
if config.getoption("--fail-fast") and session.testsfailed > 0:
session.shouldstop = "Fail-fast activated."
# ----- Max Failures -----
max_failures = config.getoption("--max-failures")
if max_failures:
try:
max_failures = int(max_failures)
if session.testsfailed >= max_failures:
session.shouldstop = f"Max failure limit {max_failures} reached."
except ValueError:
pass
# ----- Execution Metrics -----
env = config.getoption("--env")
browser_mode = "headless" if config.getoption("--headless") else "headed"
summary = metrics.generate_summary(env, browser_mode)
path = metrics.export_summary(summary)
print("\n==== EXECUTION SUMMARY ====")
print(summary)
print(f"\nReport saved at: {path}")
# ----- CI Mode Strict Validation -----
if config.getoption("--ci-mode"):
if session.testscollected == 0:
raise RuntimeError("CI Mode: No tests were collected.")
if session.testsfailed > 0:
raise RuntimeError("CI Mode: Test failures detected.")
# ----- Flaky Test Summary -----
flaky_tests = flaky_tracker.get_flaky_tests()
summary["flaky_tests"] = flaky_tests
# ----- RUN METADATA GENERATION -----
metadata = run_metadata_manager.generate_metadata(env, browser_mode)
metadata_path = run_metadata_manager.export_metadata(metadata)
print("\n==== RUN METADATA ====")
print(metadata)
print(f"Run metadata saved at: {metadata_path}")
# ----- TAG REGISTRY EXPORT -----
tag_registry_path = tag_manager.export_tags()
print("\n==== TAG REGISTRY ====")
print(f"Tag registry saved at: {tag_registry_path}")
# ----- TAG REGISTRY EXPORT -----
tag_registry_path = tag_manager.export_tags()
print("\n==== TAG REGISTRY ====")
print(f"Tag registry saved at: {tag_registry_path}")
# ---------------- TAG COLLECTION HOOK ----------------
def pytest_collection_modifyitems(config, items):
selected_tag = config.getoption("--tag")
for item in items:
tags = []
if "smoke" in item.keywords:
tags.append("smoke")
if "regression" in item.keywords:
tags.append("regression")
if "sanity" in item.keywords:
tags.append("sanity")
tag_manager.register_test(item.name, tags)
if selected_tag:
filtered = []
for item in items:
if selected_tag in item.keywords:
filtered.append(item)
items[:] = filtered
if config.getoption("--impact"):
changed = impact_analyzer.detect_changed_files()
affected = impact_analyzer.map_tests()
if not affected:
return
filtered = []
for item in items:
for test_path in affected:
if test_path in str(item.fspath):
filtered.append(item)
items[:] = filtered
# ----- IMPACT ANALYSIS REPORT -----
impact_path = impact_analyzer.export_report()
print("\n==== TEST IMPACT ANALYSIS ====")
print(f"Impact report saved at: {impact_path}")
def pytest_collection_finish(session):
dependency_graph_builder.build_graph()
# ----- DEPENDENCY GRAPH EXPORT -----
graph_path = dependency_graph_builder.export_graph()
print("\n==== DEPENDENCY GRAPH ====")
print(f"Dependency graph saved at: {graph_path}")
# ----- FAILURE ROOT CAUSE REPORT -----
failure_report_path = failure_root_analyzer.export_report()
print("\n==== FAILURE ROOT ANALYSIS ====")
print(f"Failure analysis saved at: {failure_report_path}")