-
Notifications
You must be signed in to change notification settings - Fork 0
/
beatify.py
261 lines (205 loc) · 8.84 KB
/
beatify.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
150
151
152
153
154
155
156
157
158
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/python
# -*- coding: utf-8 -*-
''' Beatify, a Behat HTML Reports condenser
Author : Gwenegan Hudin <[email protected]>
Originally made to merge all reports under a Jenkins multi-configuration Behat job into one,
pointing out which steps/scenarios failed over any of the profiles, and the list of failing
profiles for each step.
Just run python beatify.py in Jenkins project's workspace root.
WARNING : This script needs lxml to be run.
You can get it by running "sudo apt-get install python-lxml" on Ubuntu.
'''
import os
import sys
import zipfile
try:
from lxml.html import parse, etree, tostring
except ImportError:
sys.exit("Missing \"lxml\" dependancy. Please install it and try again.")
profiles = os.listdir('profile')
failed_steps = []
failed_scenarios = []
# Get which scenario this step is in
def get_ancestor_scenario(step):
for ancestor in step.iterancestors():
if ancestor.tag != 'div':
continue
tag_class = ancestor.attrib['class']
if ('scenario' in tag_class or 'scenario outline' in tag_class or
'scenario background' in tag_class):
return ancestor
# Keep trace of which and how many scenarios/steps failed over all profiles
def update_counters(failed_step):
global failed_steps
global failed_scenarios
if not failed_step in failed_steps:
failed_steps.append(failed_step)
scenario = get_ancestor_scenario(failed_step)
if not scenario in failed_scenarios:
failed_scenarios.append(scenario)
# Simply copy the first report, it will be our starting point
tree = parse('profile/' + profiles[0] + '/reports/' + 'report_' + profiles[0] + '.html')
# Store each and every example case (passed or failed)
cases = tree.xpath('//tr[@class="passed"] | //tr[@class="failed"] | //li[@class="passed"] | //li[@class="failed"]')
# For each example, check if it is failed
for case in cases:
# Prepare the list entry for this profile
entry_setup = etree.Element('li')
entry_setup.text = profiles[0]
# Skip passed steps
if case.attrib['class'] == 'passed':
continue
# Is it a standalone fail step ?
standalone = case.tag == 'li'
# Create the list of failing browsers
browser_list = etree.Element('ul')
browser_list.insert(0, entry_setup)
if standalone:
# Replace the "pre" tag by a new "div" with the failing browsers list
error = case.find('pre')
fail_div = etree.Element('div')
fail_div.insert(0, browser_list)
case.replace(error, fail_div)
else:
fail_row = case.getnext()
cell = fail_row.find('td')
# Get the stacktrace info
error = cell.find('pre[@class="backtrace"]')
# Create the list of the failing browsers
browser_list = etree.Element('ul')
browser_list.insert(0, entry_setup)
cell.insert(0, browser_list)
entry_setup.insert(0, error)
# Add error info
if error == None:
error = etree.Element('pre', {'class':'backtrace'})
error.text = 'No info'
entry_setup.insert(0, error)
# Update counters
update_counters(case)
# For each remaining profile folder, parse its report. If no report found, pass
for profile in profiles[1:]:
try:
report = parse('profile/' + profile + '/reports/' + 'report_' + profile + '.html')
except IOError:
continue
# Get all examples (in scenario outlines)
examples = report.xpath('//div[@class="examples"]')
# Store this profile's example cases
profilecases = report.xpath('//tr[@class="passed"] | //tr[@class="failed"] | //li[@class="passed"] | //li[@class="failed"]')
for i in range(0, len(profilecases)):
# Prepare the list entry for this profile
entry = etree.Element('li')
entry.text = profile
# Is it a standalone fail step ?
standalone = profilecases[i].tag == 'li'
if 'passed' in cases[i].attrib['class'] and 'failed' in profilecases[i].attrib['class']:
parent = cases[i].getparent()
# Create the list of failing browsers
browser_list = etree.Element('ul')
browser_list.insert(0, entry)
if standalone:
# Replace the "pre" tag by a new "div" with the failing browsers list
error = profilecases[i].find('pre')
fail_div = etree.Element('div')
fail_div.insert(0, browser_list)
profilecases[i].replace(error, fail_div)
else:
# Get the error message
error = profilecases[i].getnext().find('td/pre[@class="backtrace"]')
# Create the "failed exception" row
fail_row = etree.Element('tr', {'class':'failed exception'})
# Create the container cell
cell = etree.Element('td', colspan='2')
# Insert list in the cell
cell.insert(0, browser_list)
# Insert the cell in the row
fail_row.insert(0, cell)
parent.insert(parent.index(cases[i]) + 1, fail_row)
# Add error info
if error == None:
error = etree.Element('pre', {'class':'backtrace'})
error.text = 'No info'
entry.insert(0, error)
# Update the original tree
parent.replace(cases[i], profilecases[i])
cases[i] = profilecases[i]
update_counters(cases[i])
elif 'failed' in cases[i].attrib['class'] and 'failed' in profilecases[i].attrib['class']:
if standalone:
# Get the new error and insert it
error = profilecases[i].find('pre')
entry.insert(0, error)
# Get "failed" div (last child) and update its failing browsers list
fail_div = cases[i][len(cases[i])-1]
browser_list = fail_div.find('ul')
browser_list.insert(len(browser_list), entry)
else:
# Get the error message, there can be no error message on the original report !
error = profilecases[i].getnext().find('td/pre[@class="backtrace"]')
if error == None:
error = etree.Element('pre', {'class':'backtrace'})
error.text = 'No info'
entry.insert(0, error)
# Get "failed exception" row (nearest sibling) and update its failing browser list
fail_row = cases[i].getnext()
cell = fail_row.find('td')
browser_list = cell.find('ul')
browser_list.insert(len(browser_list), entry)
update_counters(cases[i])
# Update Test summary
if len(failed_steps):
# Let's factorize the code
def update_summary_part(counter, failings):
total = int(counter.text.split()[0])
counts = counter.findall('strong')
# Update failed counter
# If in the base report everything passed
if len(counts) == 1:
failed = etree.Element('strong', {'class':'failed'})
failed.text = str(len(failings)) + ' echecs'
counts[0].addnext(failed)
counts[0].tail = ', '
else:
failed = counts[len(counts)-1]
failed.text = str(len(failings)) + ' ' + failed.text.split()[1]
# If there are skipped steps, remove those from the total
if len(counts) == 3:
total -= int(counts[1].text.split()[0])
# Update succeeded counter
success = counts[0]
success.text = str(total - len(failings)) + " " + success.text.split()[1]
counters = tree.xpath('//div[@class="counters"]')[0]
# Pass from "summary passed" to "failed" if necessary
summary = counters.getparent()
if 'passed' in summary.attrib['class']:
summary.attrib['class'] = 'summary failed'
# Update scenario counters
sce_counter = counters.find('p[@class="scenarios"]')
update_summary_part(sce_counter, failed_scenarios)
# Update step counters
step_counter = counters.find('p[@class="steps"]')
update_summary_part(step_counter, failed_steps)
# Edit report style to restrain error message's size
style = tree.getroot().find('head/style')
properties = '''
.backtrace {
max-height: 100px;
width: 800px;
overflow:auto;
}
'''
style.text += properties
# Write global report
with open('global_report.html', 'w+') as final_report_file:
final_report_file.write(tostring(tree))
# Let's zip it all ! And clean the workspace
with zipfile.ZipFile('latest_reports.zip', 'w') as myzip:
myzip.write('global_report.html')
os.remove('global_report.html')
for root, dirs, files in os.walk('profile'):
for file in files:
myzip.write(os.path.join(root, file))
# Clean screenshots
if '.png' in file:
os.remove(os.path.join(root, file))