Class: ReportBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/report_builder.rb

Overview

Main report builder class

Constant Summary collapse

COLOR =

colors corresponding to status

{
    passed: '#90ed7d',
    working: '#90ed7d',
    failed: '#f45b5b',
    broken: '#f45b5b',
    undefined: '#e4d354',
    unknown: '#e4d354',
    pending: '#f7a35c',
    skipped: '#7cb5ec'
}

Class Method Summary collapse

Class Method Details

.build_report(file_or_dir = nil, output_file_name = 'test_report', output_file_type = 'html') ⇒ Object

Parameters:

  • file_or_dir (Object) (defaults to: nil)

    json file, array of json files or path, json files path

  • output_file_name (String) (defaults to: 'test_report')

    Output file name, by default test_report

  • output_file_type (String) (defaults to: 'html')

    Output file type, by default html, other options json and json_html



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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/report_builder.rb', line 47

def self.build_report(file_or_dir = nil, output_file_name = 'test_report', output_file_type = 'html')

  input = files file_or_dir
  all_features = features input rescue (raise 'ReportBuilderParsingError')

  if output_file_type.include? 'json'
    File.open(output_file_name + '.json', "w") do |file|
      file.write JSON.pretty_generate all_features
    end
    puts "JSON test report generated: '#{output_file_name}.json'"
  end

  return unless output_file_type.include? 'html'

  all_scenarios = scenarios all_features
  all_steps = steps all_scenarios
  total_time = total_time all_features
  feature_data = data all_features
  scenario_data = data all_scenarios
  step_data = data all_steps

  file = File.open(output_file_name + '.html', 'w:UTF-8')

  builder = Builder::XmlMarkup.new(:target => file, :indent => 0)
  builder.declare!(:DOCTYPE, :html)
  builder << '<html>'

  builder.head do
    builder.meta(charset: 'UTF-8')
    builder.title 'Test Results'

    builder.style(:type => 'text/css') do
      builder << File.read(File.dirname(__FILE__) + '/../vendor/assets/stylesheets/jquery-ui.min.css')
      COLOR.each do |color|
        builder << ".#{color[0].to_s}{background:#{color[1]};color:#434348;padding:2px}"
      end
      builder << '.summary{border: 1px solid #c5c5c5;border-radius:4px;text-align:right;background:#f1f1f1;color:#434348;padding:4px}'
    end

    builder.script(:type => 'text/javascript') do
      %w(jquery-min jquery-ui.min highcharts highcharts-3d).each do |file|
        builder << File.read(File.dirname(__FILE__) + '/../vendor/assets/javascripts/' + file + '.js')
      end
      builder << '$(function(){$("#results").tabs();});'
      builder << "$(function(){$('#features').accordion({collapsible: true, heightStyle: 'content', active: false, icons: false});});"
      (0..all_scenarios.size).each do |n|
        builder << "$(function(){$('#scenario#{n}').accordion({collapsible: true, heightStyle: 'content', active: false, icons: false});});"
      end
      builder << "$(function(){$('#status').accordion({collapsible: true, heightStyle: 'content', active: false, icons: false});});"
      scenario_data.each do |data|
        builder << "$(function(){$('##{data[:name]}').accordion({collapsible: true, heightStyle: 'content', active: false, icons: false});});"
      end
    end
  end

  builder << '<body>'

  builder.h4(:class => 'summary') do
    builder << all_features.size.to_s + ' feature ('
    feature_data.each do |data|
      builder << ' ' + data[:count].to_s + ' ' + data[:name]
    end
    builder << ') ~ ' + all_scenarios.size.to_s + ' scenario ('
    scenario_data.each do |data|
      builder << ' ' + data[:count].to_s + ' ' + data[:name]
    end
    builder << ') ~ ' + all_steps.size.to_s + ' step ('
    step_data.each do |data|
      builder << ' ' + data[:count].to_s + ' ' + data[:name]
    end
    builder << ') ~ ' + duration(total_time).to_s
  end

  builder.div(:id => 'results') do

    builder.ul do
      %w(overview features scenarios errors).each do |tab|
        builder.li do
          builder.a(:href => "##{tab}Tab") do
            builder << tab.capitalize
          end
        end
      end
    end

    builder.div(:id => 'overviewTab') do
      builder << "<div id='featurePieChart'></div>"
      builder << "<div id='scenarioPieChart'></div>"
      builder << "<div id='stepPieChart'></div>"
    end

    builder.div(:id => 'featuresTab') do
      builder.div(:id => 'features') do
        all_features.each_with_index do |feature, n|
          builder.h3 do
            builder.span(:class => feature['status']) do
              builder << "<strong>#{feature['keyword']}</strong> #{feature['name']} (#{duration(feature['duration'])})"
            end
          end
          builder.div do
            builder.div(:id => "scenario#{n}") do
              feature['elements'].each do |scenario|
                builder.h3 do
                  builder.span(:class => scenario['status']) do
                    builder << "<strong>#{scenario['keyword']}</strong> #{scenario['name']} (#{duration(scenario['duration'])})"
                  end
                end
                builder.div do
                  scenario['before'].each do |before|
                    if before['status'] == 'failed'
                      builder << "<strong style=color:#{COLOR[:failed]}>Error: </strong>"
                      error = before['result']['error_message'].split("\n")
                      builder.span(:style => "color:#{COLOR[:failed]}") do
                        error[0..-2].each do |line|
                          builder << line + '<br/>'
                        end
                      end
                      builder << "<strong>Hook: </strong>#{error[-1]} <br/>"
                    end
                  end
                  scenario['steps'].each do |step|
                    builder.span(:class => step['status']) do
                      builder << "<strong>#{step['keyword']}</strong> #{step['name']} (#{duration(step['duration'])})"
                    end
                    step['output'].each do |output|
                      builder << "<br/> <span style='color:#{COLOR[:skipped]}'>#{output}</span>"
                    end if step['output'] && step['output'].is_a?(Array)
                    if step['status'] == 'failed' && step['result']['error_message']
                      builder << "<br/><strong style=color:#{COLOR[:failed]}>Error: </strong>"
                      error = step['result']['error_message'].split("\n")
                      builder.span(:style => "color:#{COLOR[:failed]}") do
                        error[0..-3].each do |line|
                          builder << line + '<br/>'
                        end
                      end
                      builder << "<strong>SD: </strong>#{error[-2]} <br/>"
                      builder << "<strong>FF: </strong>#{error[-1]}"
                    end
                    step['after'].each do |after|
                      after['output'].each do |output|
                        builder << "<br/> <span style='color:#{COLOR[:skipped]}'>#{output}</span>"
                      end if after['output'] && after['output'].is_a?(Array)
                      if after['result']['error_message']
                        builder << "<br/><strong style=color:#{COLOR[:failed]}>Error: </strong>"
                        error = after['result']['error_message'].split("\n")
                        builder.span(:style => "color:#{COLOR[:failed]}") do
                          (scenario['keyword'] == 'Scenario Outline' ? error[0..-6] : error[0..-4]).each do |line|
                            builder << line + '<br/>'
                          end
                        end
                        builder << "<strong>Hook: </strong>#{scenario['keyword'] == 'Scenario Outline' ? error[-5] : error[-3]} <br/>"
                        builder << "<strong>FF: </strong>#{error[-2]} <br/>"
                      end
                    end if step['after']
                    builder << '<br/>'
                  end
                  scenario['after'].each do |after|
                    after['output'].each do |output|
                      builder << "<br/> <span style='color:#{COLOR[:skipped]}'>#{output}</span>"
                    end if after['output'] && after['output'].is_a?(Array)
                    if after['status'] == 'failed'
                      builder << "<br/><strong style=color:#{COLOR[:failed]}>Error: </strong>"
                      error = after['result']['error_message'].split("\n")
                      builder.span(:style => "color:#{COLOR[:failed]}") do
                        error[0..-2].each do |line|
                          builder << line + '<br/>'
                        end
                      end
                      builder << "<strong>Hook: </strong>#{error[-1]}"
                    end
                  end
                end
              end
            end
          end
        end
      end
      builder << "<div id='featureTabPieChart'></div>"
    end

    builder.div(:id => 'scenariosTab') do
      builder.div(:id => 'status') do
        all_scenarios.group_by{|scenario| scenario['status']}.each do |data|
          builder.h3 do
            builder.sapn(:class => data[0]) do
              builder << "<strong>#{data[0].capitalize} scenarios (Count: #{data[1].size})</strong>"
            end
          end
          builder.div do
            builder.div(:id => data[0]) do
              data[1].each do |scenario|
                builder.h3 do
                  builder.span(:class => data[0]) do
                    builder << "<strong>#{scenario['keyword']}</strong> #{scenario['name']} (#{duration(scenario['duration'])})"
                  end
                end
                builder.div do
                  scenario['before'].each do |before|
                    if before['status'] == 'failed'
                      builder << "<strong style=color:#{COLOR[:failed]}>Error: </strong>"
                      error = before['result']['error_message'].split("\n")
                      builder.span(:style => "color:#{COLOR[:failed]}") do
                        error[0..-2].each do |line|
                          builder << line + '<br/>'
                        end
                      end
                      builder << "<strong>Hook: </strong>#{error[-1]} <br/>"
                    end
                  end
                  scenario['steps'].each do |step|
                    builder.span(:class => step['status']) do
                      builder << "<strong>#{step['keyword']}</strong> #{step['name']} (#{duration(step['duration'])})"
                    end
                    step['output'].each do |output|
                      builder << "<br/> <span style='color:#{COLOR[:skipped]}'>#{output}</span>"
                    end if step['output'] && step['output'].is_a?(Array)
                    if step['status'] == 'failed' && step['result']['error_message']
                      builder << "<br/><strong style=color:#{COLOR[:failed]}>Error: </strong>"
                      error = step['result']['error_message'].split("\n")
                      builder.span(:style => "color:#{COLOR[:failed]}") do
                        error[0..-3].each do |line|
                          builder << line + '<br/>'
                        end
                      end
                      builder << "<strong>SD: </strong>#{error[-2]} <br/>"
                      builder << "<strong>FF: </strong>#{error[-1]}"
                    end
                    step['after'].each do |after|
                      after['output'].each do |output|
                        builder << "<br/> <span style='color:#{COLOR[:skipped]}'>#{output}</span>"
                      end if after['output'] && after['output'].is_a?(Array)
                      if after['result']['error_message']
                        builder << "<br/><strong style=color:#{COLOR[:failed]}>Error: </strong>"
                        error = after['result']['error_message'].split("\n")
                        builder.span(:style => "color:#{COLOR[:failed]}") do
                          (scenario['keyword'] == 'Scenario Outline' ? error[0..-6] : error[0..-4]).each do |line|
                            builder << line + '<br/>'
                          end
                        end
                        builder << "<strong>Hook: </strong>#{scenario['keyword'] == 'Scenario Outline' ? error[-5] : error[-3]} <br/>"
                        builder << "<strong>FF: </strong>#{error[-2]} <br/>"
                      end
                    end if step['after']
                    builder << '<br>'
                  end
                  scenario['after'].each do |after|
                    after['output'].each do |output|
                      builder << "<br/> <span style='color:#{COLOR[:skipped]}'>#{output}</span>"
                    end if after['output'] && after['output'].is_a?(Array)
                    if after['status'] == 'failed'
                      builder << "<br/><strong style=color:#{COLOR[:failed]}>Error: </strong>"
                      error = after['result']['error_message'].split("\n")
                      builder.span(:style => "color:#{COLOR[:failed]}") do
                        error[0..-2].each do |line|
                          builder << line + '<br/>'
                        end
                      end
                      builder << "<strong>Hook: </strong>#{error[-1]}"
                    end
                  end
                end
              end
            end
          end
        end
      end
      builder << "<div id='scenarioTabPieChart'></div>"
    end

    builder.div(:id => 'errorsTab') do
      builder.ol do
        all_scenarios.each do |scenario|
          scenario['before'].each do |before|
            next unless before['status'] == 'failed'
            builder.li do
              error = before['result']['error_message'].split("\n")
              builder.span(:style => "color:#{COLOR[:failed]}") do
                error[0..-2].each do |line|
                  builder << line + '<br/>'
                end
              end
              builder << "<strong>Hook: </strong>#{error[-1]} <br/>"
              builder << "<strong>Scenario: </strong>#{scenario['name']}"
            end
          end
          scenario['steps'].each do |step|
            step['after'].each do |after|
              next unless after['status'] == 'failed'
              builder.li do
                error = after['result']['error_message'].split("\n")
                builder.span(:style => "color:#{COLOR[:failed]}") do
                  (scenario['keyword'] == 'Scenario Outline' ? error[0..-6] : error[0..-4]).each do |line|
                    builder << line + '<br/>'
                  end
                end
                builder << "<strong>Hook: </strong>#{scenario['keyword'] == 'Scenario Outline' ? error[-5] : error[-3]} <br/>"
                builder << "<strong>FF: </strong>#{error[-2]} <br/>"
              end
            end if step['after']
            next unless step['status'] == 'failed' && step['result']['error_message']
            builder.li do
              error = step['result']['error_message'].split("\n")
              builder.span(:style => "color:#{COLOR[:failed]}") do
                error[0..-3].each do |line|
                  builder << line + '<br/>'
                end
              end
              builder << "<strong>SD: </strong>#{error[-2]} <br/>"
              builder << "<strong>FF: </strong>#{error[-1]}"
            end
          end
          scenario['after'].each do |after|
            next unless after['status'] == 'failed'
            builder.li do
              error = after['result']['error_message'].split("\n")
              builder.span(:style => "color:#{COLOR[:failed]}") do
                error[0..-2].each do |line|
                  builder << line + '<br/>'
                end
              end
              builder << "<strong>Hook: </strong>#{error[-1]} <br/>"
              builder << "<strong>Scenario: </strong>#{scenario['name']}"
            end
          end
        end
      end
    end
  end

  builder.script(:type => 'text/javascript') do
    builder << pie_chart_js('featurePieChart', 'Features', feature_data)
    builder << donut_js('featureTabPieChart', 'Features', feature_data)
    builder << pie_chart_js('scenarioPieChart', 'Scenarios', scenario_data)
    builder << donut_js('scenarioTabPieChart', 'Scenarios', scenario_data)
    builder << pie_chart_js('stepPieChart', 'Steps', step_data)
  end

  builder << '</body>'
  builder << '</html>'

  file.close

  puts "HTML test report generated: '#{output_file_name}.html'"
  [total_time, feature_data, scenario_data, step_data]
end