-
Notifications
You must be signed in to change notification settings - Fork 2
/
html_builder.py
149 lines (133 loc) · 4.19 KB
/
html_builder.py
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# html_builder.py
import os
import os
import webbrowser
def generate_html_for_screenshots(record):
script_dir = os.path.dirname(os.path.abspath(__file__))
timestamp = record['timestamp']
scenario_id = record['scenario_id']
screenshots_dir_path = os.path.join(script_dir, f"/output/{timestamp}/{scenario_id}/screenshots/".lstrip("/"))
# Get all image files from the folder
image_extensions = (".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp")
images = [img for img in os.listdir(screenshots_dir_path) if img.lower().endswith(image_extensions)]
images = sorted(images)
if not images:
print("No images found in the specified folder.")
return
# Create an HTML file to display the images
html_content = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Gallery</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
img {
margin: 10px;
max-width: 300px;
max-height: 300px;
display: inline-block;
}
</style>
</head>
<body>
<h1>Image Gallery</h1>
"""
for image in images:
image_path = os.path.join(screenshots_dir_path, image).replace("\\", "/")
html_content += f'<img src="file:///{image_path}" alt="{image}">\n'
html_content += """
</body>
</html>
"""
# Write the HTML content to a temporary file
output_file = os.path.join(screenshots_dir_path, "index.html")
with open(output_file, "w") as file:
file.write(html_content)
return output_file
def generate_html(data, output_file="output.html"):
"""
Generates an HTML file from JSON data.
:param data: List of dictionaries containing the data to display in the table.
:param output_file: Name of the HTML file to create.
"""
html_content = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test Scenarios Summary</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 10px;
text-align: left;
}
th {
background-color: #f4f4f4;
}
tr:nth-child(even) {
background-color: #f9f9f9;
}
tr:hover {
background-color: #f1f1f1;
}
</style>
</head>
<body>
<h1>Test Scenarios Summary</h1>
<table>
<thead>
<tr>
<th>Secnario ID</th>
<th>Description</th>
<th>Text Logs</th>
<th>Screenshots</th>
<th>Execution Result</th>
</tr>
</thead>
"""
for record in data:
screenshots_filepath = generate_html_for_screenshots(record)
html_content += f"""
<tr>
<td>{record['scenario_id']}</td>
<td>{record['scenario_description']}</td>
<td>
<a href={record['log_url']}>
View
</a>
</td>
<td>
<a href="file://{screenshots_filepath}">
View
</a>
</td>
<td>{record['execution_result']}</td>
</tr>
"""
html_content += """
</tbody>
</table>
</body>
</html>
"""
# Write the generated HTML content to the output file
with open(output_file, "w") as file:
file.write(html_content)
print(f"HTML file generated: {output_file}")