-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathconftest.py
More file actions
100 lines (80 loc) · 3.05 KB
/
conftest.py
File metadata and controls
100 lines (80 loc) · 3.05 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
import os
import atexit
import io
from bs4 import BeautifulSoup
import pytest
import logging
from config.constants import URL
from playwright.sync_api import sync_playwright
@pytest.fixture(scope="session")
def login_logout():
# perform login and browser close once in a session
with sync_playwright() as p:
browser = p.chromium.launch(headless=False, args=["--start-maximized"])
context = browser.new_context(no_viewport=True)
context.set_default_timeout(80000)
page = context.new_page()
# Navigate to the login URL
page.goto(URL, wait_until="domcontentloaded")
# login to web url with username and password
# login_page = LoginPage(page)
# load_dotenv()
# login_page.authenticate(os.getenv('user_name'), os.getenv('pass_word'))
yield page
# perform close the browser
browser.close()
log_streams = {}
@pytest.hookimpl(tryfirst=True)
def pytest_runtest_setup(item):
# Prepare StringIO for capturing logs
stream = io.StringIO()
handler = logging.StreamHandler(stream)
handler.setLevel(logging.INFO)
logger = logging.getLogger()
logger.addHandler(handler)
# Save handler and stream
log_streams[item.nodeid] = (handler, stream)
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
handler, stream = log_streams.get(item.nodeid, (None, None))
if handler and stream:
# Make sure logs are flushed
handler.flush()
log_output = stream.getvalue()
# Only remove the handler, don't close the stream yet
logger = logging.getLogger()
logger.removeHandler(handler)
# Store the log output on the report object for HTML reporting
report.description = f"<pre>{log_output.strip()}</pre>"
# Clean up references
log_streams.pop(item.nodeid, None)
else:
report.description = ""
def pytest_collection_modifyitems(items):
for item in items:
if hasattr(item, 'callspec'):
prompt = item.callspec.params.get("prompt")
if prompt:
item._nodeid = prompt # This controls how the test name appears in the report
def rename_duration_column():
report_path = os.path.abspath("report.html") # or your report filename
if not os.path.exists(report_path):
print("Report file not found, skipping column rename.")
return
with open(report_path, 'r', encoding='utf-8') as f:
soup = BeautifulSoup(f, 'html.parser')
# Find and rename the header
headers = soup.select('table#results-table thead th')
for th in headers:
if th.text.strip() == 'Duration':
th.string = 'Execution Time'
#print("Renamed 'Duration' to 'Execution Time'")
break
else:
print("'Duration' column not found in report.")
with open(report_path, 'w', encoding='utf-8') as f:
f.write(str(soup))
# Register this function to run after everything is done
atexit.register(rename_duration_column)