Created
May 22, 2024 05:43
-
-
Save adiralashiva8/6cae178e9c6e17514680c86c7b16148d to your computer and use it in GitHub Desktop.
generate custom html report by parsing multiple output.xml in current folder, which have result stats, tag stats in report (can be used to send email by attaching html content in email body)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from robot.api import ExecutionResult | |
from jinja2 import Template | |
from datetime import date | |
today = date.today() | |
now_date = today.strftime("%d-%b-%y") | |
# get all output.xml files in current path | |
output_names = [] | |
list_of_suites = set() | |
for r, d, f in os.walk("."): | |
for file in f: | |
if file.lower().endswith("output.xml"): | |
list_of_suites.add(os.path.join(r, file)) | |
# create object for all output.xml files | |
result = ExecutionResult(*list_of_suites) | |
stats = result.statistics | |
# get total stats | |
total_stats = stats.total | |
my_results = { | |
"total": total_stats.total, | |
"passed" : total_stats.passed, | |
"failed": total_stats.failed, | |
"skipped" : total_stats.skipped, | |
'pass_perc':round((total_stats.passed/total_stats.total)*100, 2) | |
} | |
# print(my_results) | |
# get tag stats | |
tag_stats = stats.tags.tags | |
my_tag_stats = [] | |
for tag_name, tag_stat in tag_stats.items(): | |
if "ITDFT-" not in tag_name: | |
local_list = { | |
'name':tag_name, | |
'total':tag_stat.total, | |
'passed':tag_stat.passed, | |
'failed':tag_stat.failed, | |
'skipped':tag_stat.skipped, | |
'pass_perc':round((tag_stat.passed/tag_stat.total)*100, 2) | |
} | |
my_tag_stats.append(local_list) | |
# print(my_tag_stats) | |
# create html report | |
template = Template(""" | |
<table style="width:100%"> | |
<tbody> | |
<tr> | |
<td style="width:100%"> | |
<table width="100%"> | |
<tbody> | |
<tr> | |
<td style="width:80%"> | |
<h2 style="color:#00B5E2">--Project Name--</h2> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
<table width="100%" border="0" cellspacing="0" cellpadding="0"> | |
<tbody> | |
<tr> | |
<td class="x_separator" | |
style="border-bottom:4px solid #000000; font-size:0pt; line-height:0pt"> </td> | |
</tr> | |
</tbody> | |
</table> | |
</td> | |
</tr> | |
<tr> | |
<td style="width:100%"> | |
<table width="100%"> | |
<tbody> | |
<tr> | |
<td style="width:100%"> | |
<table width="100%"> | |
<tbody> | |
<tr> | |
<td style="width:60%"> | |
<h3 style="color:blue">Automation Report</h3> | |
</td> | |
<td style="width:40%; text-align:right"> | |
<h4 style="font-size:11pt">Execution Date: {{now_date}}</h4> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
</td> | |
</tr> | |
<tr> | |
<td style="width:100%"> | |
<table cellpadding="10" cellspacing="2" border="1" width="100%" style="text-align:center"> | |
<tbody> | |
<tr style="font-weight:bold"> | |
<td style="width:20%; background:#5F9EA0">Total</td> | |
<td style="width:20%; background:#5F9EA0">Pass</td> | |
<td style="width:20%; background:#5F9EA0">Fail</td> | |
<td style="width:20%; background:#5F9EA0">Skip</td> | |
<td style="width:20%; background:#5F9EA0">Pass %</td> | |
</tr> | |
<tr> | |
<td style="width:20%"><b>{{ my_results['total'] }}</b></td> | |
<td style="width:20%; color:green"><b>{{ my_results['passed'] }}</b></td> | |
<td style="width:20%; color:red"><b>{{ my_results['failed'] }}</b></td> | |
<td style="width:20%; color:DarkGoldenrod"><b>{{ my_results['skipped'] }}</b></td> | |
<td style="width:20%; color:green"><b>{{ my_results['pass_perc'] }}</b></td> | |
</tr> | |
</tbody> | |
</table> | |
</td> | |
</tr> | |
<tr> | |
<td style="width:100%"> | |
<table width="100%" style="padding:10px 10px 10px"></table> | |
<h3 style="font-size:11pt">Statistics by Tag: </h3> | |
</td> | |
</tr> | |
<tr> | |
<td style="width:100%"> | |
<table cellpadding="10" cellspacing="2" border="1" width="100%" | |
style="text-align:left; font-size:9.5pt"> | |
<tbody> | |
<tr> | |
<td style="width:50%; background:#D3D3D3"><b>Tag Name</b></td> | |
<td style="width:12%; background:#D3D3D3; text-align:center"><b>Total</b></td> | |
<td style="width:12%; background:#D3D3D3; text-align:center"><b>Pass</b></td> | |
<td style="width:12%; background:#D3D3D3; text-align:center"><b>Fail</b></td> | |
<td style="width:12%; background:#D3D3D3; text-align:center"><b>Skip</b></td> | |
<td style="width:12%; background:#D3D3D3; text-align:center"><b>Pass Percentage</b></td> | |
</tr> | |
{% for tag_stats in my_tag_stats %} | |
<tr> | |
<td style="word-wrap:break-word">{{ tag_stats['name'] }}</td> | |
<td style="text-align:center; color:black">{{ tag_stats['total'] }}</td> | |
<td style="text-align:center; color:green">{{ tag_stats['passed'] }}</td> | |
<td style="text-align:center; color:red">{{ tag_stats['failed'] }}</td> | |
<td style="text-align:center; color:DarkGoldenrod">{{ tag_stats['skipped'] }}</td> | |
<td style="text-align:center; color:green">{{ tag_stats['pass_perc'] }}</td> | |
</tr> | |
{% endfor %} | |
</tbody> | |
</table> | |
</td> | |
</tr> | |
<tr> | |
<td style="width:100%"> | |
<table width="100%" style="padding:10px 10px 10px"></table> | |
</td> | |
</tr> | |
<tr> | |
<td style="width:100%; background:#3f3844"> | |
<table width="100%" style="padding:7px 7px 7px; color:#c3c3c3"> | |
<tbody> | |
<tr> | |
<td style="color:#B0C4DE"> | |
<p style="font-style:italic">***Note: This is an auto generated report through Jenkins, | |
please refer logs for detailed results </p> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
""") | |
# Render the template with a list of variables | |
html = template.render(my_results = my_results, my_tag_stats = my_tag_stats, now_date = now_date) | |
# Write the HTML to a file | |
with open("index.html", "w") as f: | |
f.write(html) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment