1- import os
2-
1+ from pathlib import Path
32import pytest
4- from config .constants import URL
53from playwright .sync_api import sync_playwright
6- from py .xml import html # type: ignore
4+ from config .constants import *
5+ from slugify import slugify
6+ from dotenv import load_dotenv
7+ import os
8+ from py .xml import html # type: ignore
9+ import io
10+ import logging
11+ from bs4 import BeautifulSoup
12+ import atexit
713
814
915@pytest .fixture (scope = "session" )
@@ -13,11 +19,16 @@ def login_logout():
1319 browser = p .chromium .launch (headless = False , args = ["--start-maximized" ])
1420 context = browser .new_context (no_viewport = True )
1521 context .set_default_timeout (120000 )
22+ context .clear_cookies ()
1623 page = context .new_page ()
1724 # Navigate to the login URL
1825 page .goto (URL )
1926 # Wait for the login form to appear
2027 # page.wait_for_load_state('networkidle')
28+ # login to web url with username and password
29+ #login_page = LoginPage(page)
30+ #load_dotenv()
31+ #login_page.authenticate(os.getenv('user_name'),os.getenv('pass_word'))
2132 yield page
2233
2334 # perform close the browser
@@ -26,26 +37,77 @@ def login_logout():
2637
2738@pytest .hookimpl (tryfirst = True )
2839def pytest_html_report_title (report ):
29- report .title = "Automation_DOCGEN"
40+ report .title = "Test Automation DocGen"
41+
3042
43+ log_streams = {}
3144
32- # Add a column for descriptions
33- def pytest_html_results_table_header (cells ):
34- cells .insert (1 , html .th ("Description" ))
45+ @pytest .hookimpl (tryfirst = True )
46+ def pytest_runtest_setup (item ):
47+ # Prepare StringIO for capturing logs
48+ stream = io .StringIO ()
49+ handler = logging .StreamHandler (stream )
50+ handler .setLevel (logging .INFO )
3551
52+ logger = logging .getLogger ()
53+ logger .addHandler (handler )
3654
37- def pytest_html_results_table_row (report , cells ):
38- cells .insert (
39- 1 , html .td (report .description if hasattr (report , "description" ) else "" )
40- )
55+ # Save handler and stream
56+ log_streams [item .nodeid ] = (handler , stream )
4157
4258
43- # Add logs and docstring to report
4459@pytest .hookimpl (hookwrapper = True )
4560def pytest_runtest_makereport (item , call ):
4661 outcome = yield
4762 report = outcome .get_result ()
48- report .description = str (item .function .__doc__ )
49- os .makedirs ("logs" , exist_ok = True )
50- extra = getattr (report , "extra" , [])
51- report .extra = extra
63+
64+ handler , stream = log_streams .get (item .nodeid , (None , None ))
65+
66+ if handler and stream :
67+ # Make sure logs are flushed
68+ handler .flush ()
69+ log_output = stream .getvalue ()
70+
71+ # Only remove the handler, don't close the stream yet
72+ logger = logging .getLogger ()
73+ logger .removeHandler (handler )
74+
75+ # Store the log output on the report object for HTML reporting
76+ report .description = f"<pre>{ log_output .strip ()} </pre>"
77+
78+ # Clean up references
79+ log_streams .pop (item .nodeid , None )
80+ else :
81+ report .description = ""
82+
83+ def pytest_collection_modifyitems (items ):
84+ for item in items :
85+ if hasattr (item , 'callspec' ):
86+ prompt = item .callspec .params .get ("prompt" )
87+ if prompt :
88+ item ._nodeid = prompt # This controls how the test name appears in the report
89+
90+ def rename_duration_column ():
91+ report_path = os .path .abspath ("report.html" ) # or your report filename
92+ if not os .path .exists (report_path ):
93+ print ("Report file not found, skipping column rename." )
94+ return
95+
96+ with open (report_path , 'r' , encoding = 'utf-8' ) as f :
97+ soup = BeautifulSoup (f , 'html.parser' )
98+
99+ # Find and rename the header
100+ headers = soup .select ('table#results-table thead th' )
101+ for th in headers :
102+ if th .text .strip () == 'Duration' :
103+ th .string = 'Execution Time'
104+ #print("Renamed 'Duration' to 'Execution Time'")
105+ break
106+ else :
107+ print ("'Duration' column not found in report." )
108+
109+ with open (report_path , 'w' , encoding = 'utf-8' ) as f :
110+ f .write (str (soup ))
111+
112+ # Register this function to run after everything is done
113+ atexit .register (rename_duration_column )
0 commit comments