Summarize_Newman_Html_Reports.py 10.8 KB
Newer Older
1
2
#!/usr/bin/env python3

Denis Karpenok (EPAM)'s avatar
Denis Karpenok (EPAM) committed
3
import csv
4
import os
Denis Karpenok (EPAM)'s avatar
Denis Karpenok (EPAM) committed
5
6
7
8
9
10
11
12
13
14
from time import strftime, gmtime

from openpyxl import Workbook, load_workbook
from bs4 import BeautifulSoup as BS
from openpyxl.styles import Font, PatternFill, Alignment


envs = []
collections = []

Denis Karpenok (EPAM)'s avatar
Denis Karpenok (EPAM) committed
15
for result_file in sorted(os.listdir('.')):
Denis Karpenok (EPAM)'s avatar
Denis Karpenok (EPAM) committed
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
    if result_file.endswith('.html'):
        result_file_parts = result_file.split('.')
        if result_file_parts[0] not in envs:
            envs.append(result_file_parts[0])
            envs.sort()

        if result_file_parts[1] not in collections:
            collections.append(result_file_parts[1])
            collections.sort()

result_file = Workbook()
result_data = result_file.active
result_data.title = 'Data'

result_data.cell(2, 1).value = 'Collection Name'
result_data.cell(2, 1).font = Font(bold=True)
result_data.cell(2, 1).fill = PatternFill("solid", fgColor="00CCCCFF")
result_data.cell(2, 1).alignment = Alignment(horizontal='center')

data_row = 3
ordered_collections = {}
for collection in collections:
    result_data.cell(data_row, 1).value = collection.split('CI-CD')[0].replace('_', ' ')
    result_data.cell(data_row, 1).font = Font(bold=True)
    ordered_collections[collection] = data_row
    data_row += 1

total_row_number = max([i for i in ordered_collections.values()]) + 1
result_data.cell(total_row_number, 1).value = 'Total'
result_data.cell(total_row_number, 1).font = Font(bold=True)
result_data.cell(total_row_number, 1).fill = PatternFill("solid", fgColor="d2e9c3")

env_number = 0
for env in envs:
    env_columns = 5 * env_number
    result_data.cell(1, 2 + env_columns).value = env
    result_data.merge_cells(start_row=1, end_row=1,
                            start_column=(2 + env_columns), end_column=(5 + env_columns))

    result_data.cell(1, 2 + env_columns).font = Font(size=14)
    result_data.cell(1, 2 + env_columns).fill = PatternFill("solid", fgColor="009999FF")
    result_data.cell(1, 2 + env_columns).alignment = Alignment(horizontal='center')

    result_data.cell(2, 2 + env_columns).value = 'Requests'
    result_data.cell(2, 3 + env_columns).value = 'Executed'
    result_data.cell(2, 4 + env_columns).value = 'Failed'
    result_data.cell(2, 5 + env_columns).value = 'Pass Rate'

    result_data.cell(2, 2 + env_columns).font = Font(bold=True)
    result_data.cell(2, 2 + env_columns).fill = PatternFill("solid", fgColor="00CCCCFF")
    result_data.cell(2, 2 + env_columns).alignment = Alignment(horizontal='center')
    result_data.cell(2, 3 + env_columns).font = Font(bold=True)
    result_data.cell(2, 3 + env_columns).fill = PatternFill("solid", fgColor="00CCCCFF")
    result_data.cell(2, 3 + env_columns).alignment = Alignment(horizontal='center')
    result_data.cell(2, 4 + env_columns).font = Font(bold=True, color="FF0000")
    result_data.cell(2, 4 + env_columns).fill = PatternFill("solid", fgColor="00CCCCFF")
    result_data.cell(2, 4 + env_columns).alignment = Alignment(horizontal='center')
    result_data.cell(2, 5 + env_columns).font = Font(bold=True)
    result_data.cell(2, 5 + env_columns).fill = PatternFill("solid", fgColor="00CCCCFF")
    result_data.cell(2, 5 + env_columns).alignment = Alignment(horizontal='center')

Denis Karpenok (EPAM)'s avatar
Denis Karpenok (EPAM) committed
77
    for html_report in sorted(os.listdir('.')):
Denis Karpenok (EPAM)'s avatar
Denis Karpenok (EPAM) committed
78
79
80
81
82
83
84
        if html_report.endswith('.html') and env in html_report:

            executed_requests = 'N/C'
            failed_request = 'N/C'
            executed = 'N/C'
            failed = 'N/C'

Denis Karpenok (EPAM)'s avatar
Denis Karpenok (EPAM) committed
85
            html = open(html_report, 'r', encoding='utf-8').read()
Denis Karpenok (EPAM)'s avatar
Denis Karpenok (EPAM) committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
            parsed_report = BS(html, 'html.parser')
            text_data = parsed_report.find_all('div', class_='col-sm-12 mb-3')[2].get_text().split()

            for line in range(len(text_data)):
                if text_data[line] == 'Requests':
                    executed_requests = int(text_data[line + 1])
                    failed_request = int(text_data[line + 2])
                if text_data[line] == 'Assertions':
                    executed = int(text_data[line + 1])
                    failed = int(text_data[line + 2])

            if failed_request == 0:
                result_data.cell(ordered_collections[html_report.split('.')[1]], 2 + env_columns).value = \
                    executed_requests
100
            else:
Denis Karpenok (EPAM)'s avatar
Denis Karpenok (EPAM) committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
                result_data.cell(ordered_collections[html_report.split('.')[1]], 2 + env_columns).value = \
                    f'{executed_requests}/{failed_request}'
                result_data.cell(ordered_collections[html_report.split('.')[1]], 2 + env_columns).font = \
                    Font(bold=True, color='00FF0000')
            result_data.cell(ordered_collections[html_report.split('.')[1]], 2 + env_columns).fill = \
                PatternFill("solid", fgColor="d2e9c3")
            result_data.cell(ordered_collections[html_report.split('.')[1]], 2 + env_columns).alignment = \
                Alignment(horizontal='center')

            result_data.cell(ordered_collections[html_report.split('.')[1]], 3 + env_columns).value = executed
            result_data.cell(ordered_collections[html_report.split('.')[1]], 3 + env_columns).alignment = \
                Alignment(horizontal='center')
            result_data.cell(ordered_collections[html_report.split('.')[1]], 4 + env_columns).value = failed
            result_data.cell(ordered_collections[html_report.split('.')[1]], 4 + env_columns).alignment = \
                Alignment(horizontal='center')

            if failed == 'N/C':
                pass_rate = 'N/C'
            elif failed == 0:
                pass_rate = 100
                result_data.cell(ordered_collections[html_report.split('.')[1]], 5 + env_columns).font = \
                    Font(color='00339966', bold=True)
123
            else:
Denis Karpenok (EPAM)'s avatar
Denis Karpenok (EPAM) committed
124
125
126
127
128
129
130
131
132
133
134
                pass_rate = round((executed - failed) * 100 / executed, 2)
                if pass_rate < 60:
                    result_data.cell(ordered_collections[html_report.split('.')[1]], 5 + env_columns).font = \
                        Font(color='00FF0000', bold=True)
                else:
                    result_data.cell(ordered_collections[html_report.split('.')[1]], 5 + env_columns).font = Font(
                        bold=True)

            result_data.cell(ordered_collections[html_report.split('.')[1]], 5 + env_columns).value = pass_rate
            result_data.cell(ordered_collections[html_report.split('.')[1]], 5 + env_columns).alignment = \
                Alignment(horizontal='center')
Denis Karpenok (EPAM)'s avatar
Denis Karpenok (EPAM) committed
135
            """
Denis Karpenok (EPAM)'s avatar
Denis Karpenok (EPAM) committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
            raw_data_csv_file = '../pass_rate/raw_data.csv'
            headers = ['Collection', 'Date', 'Time', 'Env', 'Requests', 'Pass', 'Fail', 'Pass Rate', 'Service Version',
                       'Run Mode']
            if not os.path.isfile(raw_data_csv_file):
                with open(raw_data_csv_file, 'w', newline='') as csvfile:
                    writer = csv.DictWriter(csvfile, fieldnames=headers, delimiter=',')
                    writer.writeheader()

            with open(raw_data_csv_file, 'a', newline='') as csvfile:
                writer = csv.DictWriter(csvfile, fieldnames=headers, delimiter=',')
                writer.writerow({
                    'Collection': html_report.split('.')[1].split('CI-CD')[0].replace('_', ' '),
                    'Date': strftime("%Y/%m/%d", gmtime()),
                    'Time': strftime("%H:%M:%S", gmtime()),
                    'Env': env,
                    'Requests': executed_requests,
                    'Pass': executed - failed,
                    'Fail': failed,
                    'Pass Rate': pass_rate,
                    'Service Version': 'n/a',
                    'Run Mode': 'CI/CD'
                })
Denis Karpenok (EPAM)'s avatar
Denis Karpenok (EPAM) committed
158
            """
Denis Karpenok (EPAM)'s avatar
Denis Karpenok (EPAM) committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    total_requests = [result_data.cell(i, 2 + env_columns).value for i in range(3, total_row_number)]
    for i in range(len(total_requests)):
        if isinstance(total_requests[i], str):
            total_requests[i] = int(total_requests[i].split('/')[0])
        if total_requests[i] is None:
            total_requests[i] = 0
    result_data.cell(total_row_number, 2 + env_columns).value = sum(total_requests)
    result_data.cell(total_row_number, 2 + env_columns).font = Font(bold=True)
    result_data.cell(total_row_number, 2 + env_columns).fill = PatternFill("solid", fgColor="d2e9c3")
    result_data.cell(total_row_number, 2 + env_columns).alignment = Alignment(horizontal='center')

    total_executed = [result_data.cell(i, 3 + env_columns).value for i in range(3, total_row_number)]
    for i in range(len(total_executed)):
        if isinstance(total_executed[i], str) or total_executed[i] is None:
            total_executed[i] = 0
    result_data.cell(total_row_number, 3 + env_columns).value = sum(total_executed)
    result_data.cell(total_row_number, 3 + env_columns).font = Font(bold=True)
    result_data.cell(total_row_number, 3 + env_columns).fill = PatternFill("solid", fgColor="d2e9c3")
    result_data.cell(total_row_number, 3 + env_columns).alignment = Alignment(horizontal='center')

    total_pass = [result_data.cell(i, 4 + env_columns).value for i in range(3, total_row_number)]
    for i in range(len(total_pass)):
        if isinstance(total_pass[i], str) or total_pass[i] is None:
            total_pass[i] = 0
    result_data.cell(total_row_number, 4 + env_columns).value = sum(total_pass)
    result_data.cell(total_row_number, 4 + env_columns).font = Font(bold=True)
    result_data.cell(total_row_number, 4 + env_columns).fill = PatternFill("solid", fgColor="d2e9c3")
    result_data.cell(total_row_number, 4 + env_columns).alignment = Alignment(horizontal='center')

    total_pass_rate = [result_data.cell(i, 5 + env_columns).value for i in range(3, total_row_number)]
    for i in range(len(total_pass_rate)):
        if isinstance(total_pass_rate[i], str) or total_pass_rate[i] is None:
            total_pass_rate[i] = 0
    total_pass_rate_value = round(sum(total_pass_rate) / len(range(3, total_row_number)), 2)
    result_data.cell(total_row_number, 5 + env_columns).value = total_pass_rate_value
    if total_pass_rate_value > 90:
        result_data.cell(total_row_number, 5 + env_columns).font = Font(bold=True, color='00339966')
    elif total_pass_rate_value < 60:
        result_data.cell(total_row_number, 5 + env_columns).font = Font(bold=True, color='00FF0000')
198
    else:
Denis Karpenok (EPAM)'s avatar
Denis Karpenok (EPAM) committed
199
200
201
202
203
204
205
206
207
        result_data.cell(total_row_number, 5 + env_columns).font = Font(bold=True)

    result_data.cell(total_row_number, 5 + env_columns).fill = PatternFill("solid", fgColor="d2e9c3")
    result_data.cell(total_row_number, 5 + env_columns).alignment = Alignment(horizontal='center')

    if env_number < (len(envs) - 1):
        result_data.cell(total_row_number, 6 + env_columns).fill = PatternFill("solid", fgColor="d2e9c3")

    env_number += 1
208

Denis Karpenok (EPAM)'s avatar
Denis Karpenok (EPAM) committed
209
210
211
212
213
214
215
216
for col in result_data.columns:
    max_length = 0
    column = col[1].column_letter
    for cell in col:
        if len(str(cell.value)) > max_length:
            max_length = len(str(cell.value))
    adjusted_width = (max_length + 2)
    result_data.column_dimensions[column].width = adjusted_width
217
218


Denis Karpenok (EPAM)'s avatar
Denis Karpenok (EPAM) committed
219
result_file.save('Test_Summary.xlsx')