|
| 1 | +import datetime |
| 2 | + |
1 | 3 | import pytest |
2 | 4 | from playwright.sync_api import sync_playwright |
3 | 5 |
|
4 | 6 | from libs import CurrentExecution as ce |
5 | 7 | from libs import file_ops as fo |
6 | | -from libs.generic_constants import fixture_scope |
| 8 | +from libs.generic_constants import audit_log_paths, file_mode, fixture_scope |
7 | 9 | from libs.mavis_constants import browsers_and_devices, playwright_constants |
8 | 10 | from libs.wrappers import * |
9 | 11 |
|
@@ -88,3 +90,40 @@ def close_browser(browser, page): |
88 | 90 | page.get_by_role("button", name="Log out").click() |
89 | 91 | page.close() |
90 | 92 | browser.close() |
| 93 | + |
| 94 | + |
| 95 | +@pytest.hookimpl(tryfirst=True) |
| 96 | +def pytest_sessionstart(session): |
| 97 | + with open(audit_log_paths.TEST_LEVEL_LOG, file_mode.APPEND) as log_file: |
| 98 | + log_file.write(f"Test Session Started: {datetime.now()}\n") |
| 99 | + |
| 100 | + |
| 101 | +@pytest.hookimpl(trylast=True) |
| 102 | +def pytest_sessionfinish(session, exitstatus): |
| 103 | + with open(audit_log_paths.TEST_LEVEL_LOG, file_mode.APPEND) as log_file: |
| 104 | + log_file.write(f"Test Session Ended: {datetime.now()}\n") |
| 105 | + |
| 106 | + |
| 107 | +@pytest.hookimpl(hookwrapper=True) |
| 108 | +def pytest_runtest_logreport(report): |
| 109 | + outcome = yield |
| 110 | + |
| 111 | + if report.when == "call": # Log only actual test results |
| 112 | + test_name = report.nodeid |
| 113 | + test_result = report.outcome.upper() # 'passed', 'failed', or 'skipped' |
| 114 | + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 115 | + |
| 116 | + with open(audit_log_paths.TEST_LEVEL_LOG, file_mode.APPEND) as log_file: |
| 117 | + log_file.write(f"{timestamp} | {test_name} | {test_result}\n") |
| 118 | + |
| 119 | + |
| 120 | +# @pytest.fixture |
| 121 | +# def step_logger(): |
| 122 | +# """Fixture to log individual steps within a test.""" |
| 123 | + |
| 124 | +# def log_step(step_description): |
| 125 | +# timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 126 | +# with open(audit_log_paths.TEST_LEVEL_LOG, file_mode.APPEND) as log_file: |
| 127 | +# log_file.write(f"{timestamp} | STEP | {step_description}\n") |
| 128 | + |
| 129 | +# return log_step |
0 commit comments