|
1 |
| -import os |
2 |
| - |
3 | 1 | import pytest
|
4 |
| -from config.constants import * |
| 2 | +import os |
| 3 | +import io |
| 4 | +import logging |
| 5 | +import atexit |
| 6 | +from bs4 import BeautifulSoup |
5 | 7 | from playwright.sync_api import sync_playwright
|
6 |
| -from py.xml import html # type: ignore |
| 8 | +from config.constants import * |
7 | 9 |
|
| 10 | +log_streams = {} |
8 | 11 |
|
| 12 | +# ---------- FIXTURE: Login and Logout Setup ---------- |
9 | 13 | @pytest.fixture(scope="session")
|
10 | 14 | def login_logout():
|
11 |
| - # perform login and browser close once in a session |
12 | 15 | with sync_playwright() as p:
|
13 | 16 | browser = p.chromium.launch(headless=False, args=["--start-maximized"])
|
14 | 17 | context = browser.new_context(no_viewport=True)
|
15 | 18 | context.set_default_timeout(80000)
|
16 | 19 | page = context.new_page()
|
17 |
| - # Navigate to the login URL |
| 20 | + |
| 21 | + # Load URL and wait |
18 | 22 | page.goto(WEB_URL)
|
19 |
| - # Wait for the login form to appear |
20 | 23 | page.wait_for_load_state("networkidle")
|
21 | 24 | page.wait_for_timeout(5000)
|
22 |
| - # login to web url with username and password |
23 |
| - # login_page = LoginPage(page) |
| 25 | + |
| 26 | + # Uncomment if authentication is needed |
24 | 27 | # load_dotenv()
|
| 28 | + # login_page = LoginPage(page) |
25 | 29 | # login_page.authenticate(os.getenv('user_name'), os.getenv('pass_word'))
|
| 30 | + |
26 | 31 | yield page
|
27 | 32 | browser.close()
|
28 | 33 |
|
29 |
| - |
| 34 | +# ---------- HTML Report Title ---------- |
30 | 35 | @pytest.hookimpl(tryfirst=True)
|
31 | 36 | def pytest_html_report_title(report):
|
32 | 37 | report.title = "Test_Automation_Chat_with_your_Data"
|
33 | 38 |
|
| 39 | +# ---------- Logging Setup per Test ---------- |
| 40 | +@pytest.hookimpl(tryfirst=True) |
| 41 | +def pytest_runtest_setup(item): |
| 42 | + stream = io.StringIO() |
| 43 | + handler = logging.StreamHandler(stream) |
| 44 | + handler.setLevel(logging.INFO) |
| 45 | + logger = logging.getLogger() |
| 46 | + logger.addHandler(handler) |
| 47 | + log_streams[item.nodeid] = (handler, stream) |
34 | 48 |
|
35 |
| -# Add a column for descriptions |
36 |
| -def pytest_html_results_table_header(cells): |
37 |
| - cells.insert(1, html.th("Description")) |
38 |
| - |
39 |
| - |
40 |
| -def pytest_html_results_table_row(report, cells): |
41 |
| - cells.insert( |
42 |
| - 1, html.td(report.description if hasattr(report, "description") else "") |
43 |
| - ) |
44 |
| - |
45 |
| - |
46 |
| -# Add logs and docstring to report |
| 49 | +# ---------- Attach Logs to HTML Report ---------- |
47 | 50 | @pytest.hookimpl(hookwrapper=True)
|
48 | 51 | def pytest_runtest_makereport(item, call):
|
49 | 52 | outcome = yield
|
50 | 53 | report = outcome.get_result()
|
51 |
| - report.description = str(item.function.__doc__) |
52 |
| - os.makedirs("logs", exist_ok=True) |
53 |
| - extra = getattr(report, "extra", []) |
54 |
| - report.extra = extra |
| 54 | + |
| 55 | + if report.when == "call": |
| 56 | + question_logs = getattr(item, "_question_logs", None) |
| 57 | + if question_logs: |
| 58 | + for i, (question, logs) in enumerate(question_logs.items(), start=1): |
| 59 | + report.sections.append((f"Q{i:02d}: {question}", logs)) |
| 60 | + else: |
| 61 | + log = getattr(item, "_captured_log", None) |
| 62 | + if log: |
| 63 | + report.sections.append(("Captured Log", log)) |
| 64 | + |
| 65 | +# ---------- Optional: Clean Up Node IDs for Parametrized Prompts ---------- |
| 66 | +def pytest_collection_modifyitems(items): |
| 67 | + for item in items: |
| 68 | + if hasattr(item, 'callspec') and "prompt" in item.callspec.params: |
| 69 | + item._nodeid = item.callspec.params["prompt"] |
| 70 | + |
| 71 | +# ---------- Rename Duration Column in HTML Report ---------- |
| 72 | +def rename_duration_column(): |
| 73 | + report_path = os.path.abspath("report.html") |
| 74 | + if not os.path.exists(report_path): |
| 75 | + print("Report file not found, skipping column rename.") |
| 76 | + return |
| 77 | + |
| 78 | + with open(report_path, 'r', encoding='utf-8') as f: |
| 79 | + soup = BeautifulSoup(f, 'html.parser') |
| 80 | + |
| 81 | + headers = soup.select('table#results-table thead th') |
| 82 | + for th in headers: |
| 83 | + if th.text.strip() == 'Duration': |
| 84 | + th.string = 'Execution Time' |
| 85 | + break |
| 86 | + else: |
| 87 | + print("'Duration' column not found in report.") |
| 88 | + |
| 89 | + with open(report_path, 'w', encoding='utf-8') as f: |
| 90 | + f.write(str(soup)) |
| 91 | + |
| 92 | +atexit.register(rename_duration_column) |
0 commit comments