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') ⇒ 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



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
# File 'lib/report_builder.rb', line 46

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

  input = files file_or_dir
  all_features = features input rescue (raise 'ReportBuilderParsingError')
  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']} (#{feature['status']}) #{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']} (#{scenario['status']})  #{duration(scenario['duration'])}"
                  end
                end
                builder.div do
                  scenario['steps'].each do |step|
                    builder.span(:class => step['status']) do
                      builder << "<strong>#{step['keyword']}</strong> #{step['name']} (#{step['status']})  #{duration(step['duration'])}"
                    end
                    if step['status'] == 'failed'
                      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
                    builder << '<br/>'
                  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']} (#{data[0]}) #{duration(scenario['duration'])}"
                  end
                end
                builder.div do
                  scenario['steps'].each do |step|
                    builder.span(:class => step['status']) do
                      builder << "<strong>#{step['keyword']}</strong> #{step['name']} (#{step['status']}) #{duration(step['duration'])}"
                    end
                    if step['status'] == 'failed'
                      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
                    builder << '<br>'
                  end
                end
              end
            end
          end
        end
      end
      builder << "<div id='scenarioTabPieChart'></div>"
    end

    builder.div(:id => 'errorsTab') do
      builder.ol do
        all_steps.each do |step|
          next unless step['status'] == 'failed'
          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
      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 "Test report generated: '#{output_file_name}.html'"
end