Class: Cucumber::Formatter::Html

Inherits:
Object
  • Object
show all
Includes:
Duration, Io, ERB::Util
Defined in:
lib/cucumber/formatter/html.rb

Defined Under Namespace

Classes: SnippetExtractor

Instance Method Summary collapse

Methods included from Io

#ensure_dir, #ensure_file, #ensure_io

Methods included from Duration

#format_duration

Constructor Details

#initialize(step_mother, path_or_io, options) ⇒ Html

Returns a new instance of Html.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cucumber/formatter/html.rb', line 13

def initialize(step_mother, path_or_io, options)
  @io = ensure_io(path_or_io, "html")
  @step_mother = step_mother
  @options = options
  @buffer = {}
  @builder = create_builder(@io)
  @feature_number = 0
  @scenario_number = 0
  @step_number = 0
  @header_red = nil
  @delayed_announcements = []
end

Instance Method Details

#after_background(background) ⇒ Object



137
138
139
140
# File 'lib/cucumber/formatter/html.rb', line 137

def after_background(background)
  @in_background = nil
  @builder << '</div>'
end

#after_comment(comment) ⇒ Object



99
100
101
# File 'lib/cucumber/formatter/html.rb', line 99

def after_comment(comment)
  @builder << '</pre>'
end

#after_examples(examples) ⇒ Object



189
190
191
# File 'lib/cucumber/formatter/html.rb', line 189

def after_examples(examples)
  @builder << '</div>'
end

#after_feature(feature) ⇒ Object



91
92
93
# File 'lib/cucumber/formatter/html.rb', line 91

def after_feature(feature)
  @builder << '</div>'
end

#after_feature_element(feature_element) ⇒ Object



161
162
163
164
# File 'lib/cucumber/formatter/html.rb', line 161

def after_feature_element(feature_element)
  @builder << '</div>'
  @open_step_list = true
end

#after_features(features) ⇒ Object



79
80
81
82
83
84
# File 'lib/cucumber/formatter/html.rb', line 79

def after_features(features)
  print_stats(features)
  @builder << '</div>'
  @builder << '</body>'
  @builder << '</html>'
end

#after_multiline_arg(multiline_arg) ⇒ Object



279
280
281
282
283
284
# File 'lib/cucumber/formatter/html.rb', line 279

def after_multiline_arg(multiline_arg)
  return if @hide_this_step || @skip_step
  if Ast::Table === multiline_arg
    @builder << '</table>'
  end
end

#after_outline_table(outline_table) ⇒ Object



180
181
182
183
# File 'lib/cucumber/formatter/html.rb', line 180

def after_outline_table(outline_table)
  @builder << '</table>'
  @outline_row = nil
end

#after_step(step) ⇒ Object



215
216
217
# File 'lib/cucumber/formatter/html.rb', line 215

def after_step(step)
  move_progress
end

#after_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/cucumber/formatter/html.rb', line 239

def after_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background)
  return if @hide_this_step
  # print snippet for undefined steps
  if status == :undefined
    step_multiline_class = @step.multiline_arg ? @step.multiline_arg.class : nil
    @builder.pre do |pre|
      pre << @step_mother.snippet_text(@step.actual_keyword,step_match.instance_variable_get("@name") || '',step_multiline_class)
    end
  end
  @builder << '</li>'
  print_announcements
end

#after_steps(steps) ⇒ Object



205
206
207
# File 'lib/cucumber/formatter/html.rb', line 205

def after_steps(steps)
  @builder << '</ol>'
end

#after_table_row(table_row) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/cucumber/formatter/html.rb', line 301

def after_table_row(table_row)
  return if @hide_this_step
  print_table_row_announcements
  @builder << '</tr>'
  if table_row.exception
    @builder.tr do
      @builder.td(:colspan => @col_index.to_s, :class => 'failed') do
        @builder.pre do |pre|
          pre << format_exception(table_row.exception)
        end
      end
    end
    set_scenario_color_failed
  end
  if @outline_row
    @outline_row += 1
  end
  @step_number += 1
  move_progress
end

#after_tags(tags) ⇒ Object



108
109
110
# File 'lib/cucumber/formatter/html.rb', line 108

def after_tags(tags)
  @tag_spacer = nil
end

#announce(announcement) ⇒ Object



333
334
335
336
# File 'lib/cucumber/formatter/html.rb', line 333

def announce(announcement)
  @delayed_announcements << announcement
  #@builder.pre(announcement, :class => 'announcement')
end

#background_name(keyword, name, file_colon_line, source_indent) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/cucumber/formatter/html.rb', line 142

def background_name(keyword, name, file_colon_line, source_indent)
  @listing_background = true
  @builder.h3 do |h3|
    @builder.span(keyword, :class => 'keyword')
    @builder.text!(' ')
    @builder.span(name, :class => 'val')
  end
end

#before_background(background) ⇒ Object



132
133
134
135
# File 'lib/cucumber/formatter/html.rb', line 132

def before_background(background)
  @in_background = true
  @builder << '<div class="background">'
end

#before_comment(comment) ⇒ Object



95
96
97
# File 'lib/cucumber/formatter/html.rb', line 95

def before_comment(comment)
  @builder << '<pre class="comment">'
end

#before_examples(examples) ⇒ Object



185
186
187
# File 'lib/cucumber/formatter/html.rb', line 185

def before_examples(examples)
   @builder << '<div class="examples">'
end

#before_feature(feature) ⇒ Object



86
87
88
89
# File 'lib/cucumber/formatter/html.rb', line 86

def before_feature(feature)
  @exceptions = []
  @builder << '<div class="feature">'
end

#before_feature_element(feature_element) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/cucumber/formatter/html.rb', line 151

def before_feature_element(feature_element)
  @scenario_number+=1
  @scenario_red = false
  css_class = {
    Ast::Scenario        => 'scenario',
    Ast::ScenarioOutline => 'scenario outline'
  }[feature_element.class]      
  @builder << "<div class='#{css_class}'>"
end

#before_features(features) ⇒ Object



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
# File 'lib/cucumber/formatter/html.rb', line 42

def before_features(features)
  @step_count = get_step_count(features)

  # <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  @builder.declare!(
    :DOCTYPE,
    :html, 
    :PUBLIC, 
    '-//W3C//DTD XHTML 1.0 Strict//EN',
    'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'
  )

  @builder << '<html xmlns ="http://www.w3.org/1999/xhtml">'
    @builder.head do
    @builder.meta(:content => 'text/html;charset=utf-8')
    @builder.title 'Cucumber'
    inline_css
    inline_js
  end
  @builder << '<body>'
  @builder << "<!-- Step count #{@step_count}-->"
  @builder << '<div class="cucumber">'
  @builder.div(:id => 'cucumber-header') do
    @builder.div(:id => 'label') do
      @builder.h1('Cucumber Features')
    end
    @builder.div(:id => 'summary') do
      @builder.p('',:id => 'totals')
      @builder.p('',:id => 'duration')
      @builder.div(:id => 'expand-collapse') do
        @builder.p('Expand All', :id => 'expander')
        @builder.p('Collapse All', :id => 'collapser')
      end
    end
  end
end

#before_multiline_arg(multiline_arg) ⇒ Object



272
273
274
275
276
277
# File 'lib/cucumber/formatter/html.rb', line 272

def before_multiline_arg(multiline_arg)
  return if @hide_this_step || @skip_step
  if Ast::Table === multiline_arg
    @builder << '<table>'
  end
end

#before_outline_table(outline_table) ⇒ Object



175
176
177
178
# File 'lib/cucumber/formatter/html.rb', line 175

def before_outline_table(outline_table)
  @outline_row = 0
  @builder << '<table>'
end

#before_step(step) ⇒ Object



209
210
211
212
213
# File 'lib/cucumber/formatter/html.rb', line 209

def before_step(step)
  @step_id = step.dom_id
  @step_number += 1
  @step = step
end

#before_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/cucumber/formatter/html.rb', line 219

def before_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background)
  @step_match = step_match
  @hide_this_step = false
  if exception
    if @exceptions.include?(exception)
      @hide_this_step = true
      return
    end
    @exceptions << exception
  end
  if status != :failed && @in_background ^ background
    @hide_this_step = true
    return
  end
  @status = status
  return if @hide_this_step
  set_scenario_color(status)      
  @builder << "<li id='#{@step_id}' class='step #{status}'>"            
end

#before_steps(steps) ⇒ Object



201
202
203
# File 'lib/cucumber/formatter/html.rb', line 201

def before_steps(steps)
  @builder << '<ol>'
end

#before_table_row(table_row) ⇒ Object



294
295
296
297
298
299
# File 'lib/cucumber/formatter/html.rb', line 294

def before_table_row(table_row)
  @row_id = table_row.dom_id
  @col_index = 0
  return if @hide_this_step
  @builder << "<tr class='step' id='#{@row_id}'>"
end

#comment_line(comment_line) ⇒ Object



103
104
105
106
# File 'lib/cucumber/formatter/html.rb', line 103

def comment_line(comment_line)
  @builder.text!(comment_line)
  @builder.br
end

#embed(file, mime_type) ⇒ Object



26
27
28
29
30
31
# File 'lib/cucumber/formatter/html.rb', line 26

def embed(file, mime_type)
  case(mime_type)
  when /^image\/(png|gif|jpg|jpeg)/
    embed_image(file)
  end
end

#embed_image(file) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/cucumber/formatter/html.rb', line 33

def embed_image(file)
  id = file.hash
  @builder.span(:class => 'embed') do |pre|
    pre << %{<a href="" onclick="img=document.getElementById('#{id}'); img.style.display = (img.style.display == 'none' ? 'block' : 'none');return false">Screenshot</a><br>&nbsp;
    <img id="#{id}" style="display: none" src="#{file}"/>}
  end
end

#empty_announcementsObject



360
361
362
# File 'lib/cucumber/formatter/html.rb', line 360

def empty_announcements
  @delayed_announcements = []
end

#examples_name(keyword, name) ⇒ Object



193
194
195
196
197
198
199
# File 'lib/cucumber/formatter/html.rb', line 193

def examples_name(keyword, name)
  @builder.h4 do
    @builder.span(keyword, :class => 'keyword')
    @builder.text!(' ')
    @builder.span(name, :class => 'val')
  end
end

#exception(exception, status) ⇒ Object



263
264
265
# File 'lib/cucumber/formatter/html.rb', line 263

def exception(exception, status)
  build_exception_detail(exception)
end

#extra_failure_content(file_colon_line) ⇒ Object



267
268
269
270
# File 'lib/cucumber/formatter/html.rb', line 267

def extra_failure_content(file_colon_line)
  @snippet_extractor ||= SnippetExtractor.new
  "<pre class=\"ruby\"><code>#{@snippet_extractor.snippet(file_colon_line)}</code></pre>"
end

#feature_name(keyword, name) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/cucumber/formatter/html.rb', line 118

def feature_name(keyword, name)
  lines = name.split(/\r?\n/)
  return if lines.empty?
  @builder.h2 do |h2|
    @builder.span(keyword + ': ' + lines[0], :class => 'val')
  end
  @builder.p(:class => 'narrative') do
    lines[1..-1].each do |line|
      @builder.text!(line.strip)
      @builder.br
    end
  end
end


338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/cucumber/formatter/html.rb', line 338

def print_announcements
  return if @delayed_announcements.empty?
  
  #@builder.ol do
    @delayed_announcements.each do |ann|
      @builder.li(:class => 'step announcement') do
        @builder << ann
      end
    end
  #end
  empty_announcements
end


351
352
353
354
355
356
357
358
# File 'lib/cucumber/formatter/html.rb', line 351

def print_table_row_announcements
  return if @delayed_announcements.empty?
  
  @builder.td(:class => 'announcement') do
    @builder << @delayed_announcements.join(", ")
  end
  empty_announcements
end

#py_string(string) ⇒ Object



286
287
288
289
290
291
# File 'lib/cucumber/formatter/html.rb', line 286

def py_string(string)
  return if @hide_this_step
  @builder.pre(:class => 'val') do |pre|
    @builder << string.gsub("\n", '&#x000A;')
  end
end

#scenario_name(keyword, name, file_colon_line, source_indent) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/cucumber/formatter/html.rb', line 166

def scenario_name(keyword, name, file_colon_line, source_indent)
  @listing_background = false
  @builder.h3(:id => "scenario_#{@scenario_number}") do
    @builder.span(keyword + ':', :class => 'keyword')
    @builder.text!(' ')
    @builder.span(name, :class => 'val')
  end
end

#step_name(keyword, step_match, status, source_indent, background) ⇒ Object



252
253
254
255
256
257
258
259
260
261
# File 'lib/cucumber/formatter/html.rb', line 252

def step_name(keyword, step_match, status, source_indent, background)
  @step_matches ||= []
  background_in_scenario = background && !@listing_background
  @skip_step = @step_matches.index(step_match) || background_in_scenario
  @step_matches << step_match

  unless @skip_step
    build_step(keyword, step_match, status)
  end
end

#table_cell_value(value, status) ⇒ Object



322
323
324
325
326
327
328
329
330
331
# File 'lib/cucumber/formatter/html.rb', line 322

def table_cell_value(value, status)
  return if @hide_this_step
  
  @cell_type = @outline_row == 0 ? :th : :td
  attributes = {:id => "#{@row_id}_#{@col_index}", :class => 'step'}
  attributes[:class] += " #{status}" if status
  build_cell(@cell_type, value, attributes)
  set_scenario_color(status)
  @col_index += 1
end

#tag_name(tag_name) ⇒ Object



112
113
114
115
116
# File 'lib/cucumber/formatter/html.rb', line 112

def tag_name(tag_name)
  @builder.text!(@tag_spacer) if @tag_spacer
  @tag_spacer = ' '
  @builder.span(tag_name, :class => 'tag')
end