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

Constant Summary collapse

AST_CLASSES =

 TODO: remove coupling to types

{
  Cucumber::Core::Ast::Scenario        => 'scenario',
  Cucumber::Core::Ast::ScenarioOutline => 'scenario outline'
}
AST_DATA_TABLE =
LegacyApi::Ast::MultilineArg::DataTable

Instance Method Summary collapse

Methods included from Io

ensure_dir, ensure_file, ensure_io

Methods included from Duration

#format_duration

Constructor Details

#initialize(runtime, path_or_io, options) ⇒ Html

Returns a new instance of Html.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cucumber/formatter/html.rb', line 22

def initialize(runtime, path_or_io, options)
  @io = ensure_io(path_or_io)
  @runtime = runtime
  @options = options
  @buffer = {}
  @builder = create_builder(@io)
  @feature_number = 0
  @scenario_number = 0
  @step_number = 0
  @header_red = nil
  @delayed_messages = []
  @img_id = 0
  @text_id = 0
  @inside_outline = false
  @previous_step_keyword = nil
end

Instance Method Details

#after_background(background) ⇒ Object



173
174
175
176
# File 'lib/cucumber/formatter/html.rb', line 173

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

#after_comment(comment) ⇒ Object



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

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

#after_examples(examples) ⇒ Object



238
239
240
# File 'lib/cucumber/formatter/html.rb', line 238

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

#after_feature(feature) ⇒ Object



123
124
125
# File 'lib/cucumber/formatter/html.rb', line 123

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

#after_feature_element(feature_element) ⇒ Object



195
196
197
198
199
200
201
202
# File 'lib/cucumber/formatter/html.rb', line 195

def after_feature_element(feature_element)
  unless @in_scenario_outline
    print_messages
    @builder << '</ol>'
  end
  @builder << '</div>'
  @in_scenario_outline = nil
end

#after_features(features) ⇒ Object



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

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

#after_multiline_arg(multiline_arg) ⇒ Object



335
336
337
338
339
340
# File 'lib/cucumber/formatter/html.rb', line 335

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

#after_outline_table(outline_table) ⇒ Object



228
229
230
231
232
# File 'lib/cucumber/formatter/html.rb', line 228

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

#after_step(step) ⇒ Object



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

def after_step(step)
  move_progress
end

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



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/cucumber/formatter/html.rb', line 290

def after_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background, file_colon_line)
  return if @hide_this_step
  # print snippet for undefined steps
  unless outline_step?(@step)
    keyword = @step.actual_keyword(@previous_step_keyword)
    @previous_step_keyword = keyword
  end
  if status == :undefined
    @builder.pre do |pre|
      # TODO: snippet text should be an event sent to the formatter so we don't 
      # have this couping to the runtime.
      pre << @runtime.snippet_text(keyword,step_match.instance_variable_get("@name") || '', @step.multiline_arg)
    end
  end
  @builder << '</li>'
  print_messages
end

#after_steps(steps) ⇒ Object



254
255
256
257
# File 'lib/cucumber/formatter/html.rb', line 254

def after_steps(steps)
  print_messages
  @builder << '</ol>' if @in_background or @in_scenario_outline
end

#after_table_row(table_row) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/cucumber/formatter/html.rb', line 356

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

#after_tags(tags) ⇒ Object



140
141
142
# File 'lib/cucumber/formatter/html.rb', line 140

def after_tags(tags)
  @tag_spacer = nil
end

#after_test_case(test_case, result) ⇒ Object



423
424
425
426
427
# File 'lib/cucumber/formatter/html.rb', line 423

def after_test_case(test_case, result)
  if result.failed? and not @scenario_red
    set_scenario_color_failed
  end
end

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



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

def background_name(keyword, name, file_colon_line, source_indent)
  @listing_background = true
  @builder.h3(:id => "background_#{@scenario_number}") do |h3|
    @builder.span(keyword, :class => 'keyword')
    @builder.text!(' ')
    @builder.span(name, :class => 'val')
  end
end

#before_background(background) ⇒ Object



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

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

#before_comment(comment) ⇒ Object



127
128
129
# File 'lib/cucumber/formatter/html.rb', line 127

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

#before_examples(examples) ⇒ Object



234
235
236
# File 'lib/cucumber/formatter/html.rb', line 234

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

#before_feature(feature) ⇒ Object



118
119
120
121
# File 'lib/cucumber/formatter/html.rb', line 118

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

#before_feature_element(feature_element) ⇒ Object



187
188
189
190
191
192
193
# File 'lib/cucumber/formatter/html.rb', line 187

def before_feature_element(feature_element)
  @scenario_number+=1
  @scenario_red = false
  css_class = AST_CLASSES[feature_element.class]
  @builder << "<div class='#{css_class}'>"
  @in_scenario_outline = feature_element.class == Cucumber::Core::Ast::ScenarioOutline
end

#before_features(features) ⇒ Object



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

def before_features(features)
  @step_count = features && features.step_count || 0 #TODO: Make this work with core!

  # <!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('http-equiv' => 'Content-Type', :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



328
329
330
331
332
333
# File 'lib/cucumber/formatter/html.rb', line 328

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

#before_outline_table(outline_table) ⇒ Object



222
223
224
225
226
# File 'lib/cucumber/formatter/html.rb', line 222

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

#before_step(step) ⇒ Object



259
260
261
262
263
264
# File 'lib/cucumber/formatter/html.rb', line 259

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

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



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/cucumber/formatter/html.rb', line 270

def before_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background, file_colon_line)
  @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



250
251
252
# File 'lib/cucumber/formatter/html.rb', line 250

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

#before_table_row(table_row) ⇒ Object



349
350
351
352
353
354
# File 'lib/cucumber/formatter/html.rb', line 349

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

#before_test_case(test_case) ⇒ Object



164
165
166
# File 'lib/cucumber/formatter/html.rb', line 164

def before_test_case(test_case)
  @previous_step_keyword = nil
end

#comment_line(comment_line) ⇒ Object



135
136
137
138
# File 'lib/cucumber/formatter/html.rb', line 135

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

#doc_string(string) ⇒ Object



342
343
344
345
346
347
# File 'lib/cucumber/formatter/html.rb', line 342

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

#embed(src, mime_type, label) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cucumber/formatter/html.rb', line 39

def embed(src, mime_type, label)
  case(mime_type)
  when /^image\/(png|gif|jpg|jpeg)/
    unless File.file?(src) or src =~ /^data:image\/(png|gif|jpg|jpeg);base64,/
      type = mime_type =~ /;base[0-9]+$/ ? mime_type : mime_type + ";base64"
      src = "data:" + type + "," + src
    end
    embed_image(src, label)
  when /^text\/plain/
    embed_text(src, label)
  end
end

#embed_image(src, label) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cucumber/formatter/html.rb', line 52

def embed_image(src, label)
  id = "img_#{@img_id}"
  @img_id += 1
  if @io.respond_to?(:path) and File.file?(src)
    out_dir = Pathname.new(File.dirname(File.absolute_path(@io.path)))
    src = Pathname.new(File.absolute_path(src)).relative_path_from(out_dir)
  end        
  @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">#{label}</a><br>&nbsp;
    <img id="#{id}" style="display: none" src="#{src}"/>}
  end
end

#embed_text(src, label) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/cucumber/formatter/html.rb', line 65

def embed_text(src, label)
  id = "text_#{@text_id}"
  @text_id += 1
  @builder.span(:class => 'embed') do |pre|
    pre << %{<a id="#{id}" href="#{src}" title="#{label}">#{label}</a>}
  end
end

#empty_messagesObject



419
420
421
# File 'lib/cucumber/formatter/html.rb', line 419

def empty_messages
  @delayed_messages = []
end

#examples_name(keyword, name) ⇒ Object



242
243
244
245
246
247
248
# File 'lib/cucumber/formatter/html.rb', line 242

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



317
318
319
320
321
# File 'lib/cucumber/formatter/html.rb', line 317

def exception(exception, status)
  return if @hide_this_step
  print_messages
  build_exception_detail(exception)
end

#extra_failure_content(file_colon_line) ⇒ Object



323
324
325
326
# File 'lib/cucumber/formatter/html.rb', line 323

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



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

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


397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/cucumber/formatter/html.rb', line 397

def print_messages
  return if @delayed_messages.empty?

  #@builder.ol do
    @delayed_messages.each do |ann|
      @builder.li(:class => 'step message') do
        @builder << ann
      end
    end
  #end
  empty_messages
end


410
411
412
413
414
415
416
417
# File 'lib/cucumber/formatter/html.rb', line 410

def print_table_row_messages
  return if @delayed_messages.empty?

  @builder.td(:class => 'message') do
    @builder << @delayed_messages.join(", ")
  end
  empty_messages
end

#puts(message) ⇒ Object



392
393
394
395
# File 'lib/cucumber/formatter/html.rb', line 392

def puts(message)
  @delayed_messages << message
  #@builder.pre(message, :class => 'message')
end

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



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/cucumber/formatter/html.rb', line 204

def scenario_name(keyword, name, file_colon_line, source_indent)
  @builder.span(:class => 'scenario_file') do
    @builder << file_colon_line
  end
  @listing_background = false
  scenario_id = "scenario_#{@scenario_number}"
  if @inside_outline
    @outline_row += 1
    scenario_id += "_#{@outline_row}"
    @scenario_red = false
  end
  @builder.h3(:id => scenario_id) do
    @builder.span(keyword + ':', :class => 'keyword')
    @builder.text!(' ')
    @builder.span(name, :class => 'val')
  end
end

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



308
309
310
311
312
313
314
315
# File 'lib/cucumber/formatter/html.rb', line 308

def step_name(keyword, step_match, status, source_indent, background, file_colon_line)
  background_in_scenario = background && !@listing_background
  @skip_step = background_in_scenario

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

#table_cell_value(value, status) ⇒ Object



381
382
383
384
385
386
387
388
389
390
# File 'lib/cucumber/formatter/html.rb', line 381

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) if @inside_outline
  @col_index += 1
end

#tag_name(tag_name) ⇒ Object



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

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