class Cucumber::Formatter::Html

Constants

AST_CLASSES

TODO: remove coupling to types

AST_DATA_TABLE

Attributes

builder[R]

Public Class Methods

new(runtime, path_or_io, options) click to toggle source
# File lib/cucumber/formatter/html.rb, line 26
def initialize(runtime, path_or_io, options)
  @io = ensure_io(path_or_io)
  @runtime = runtime
  @options = options
  @buffer = {}
  @builder = HtmlBuilder.new(target: @io, indent: 0)
  @feature_number = 0
  @scenario_number = 0
  @step_number = 0
  @header_red = nil
  @delayed_messages = []
  @inside_outline        = false
  @previous_step_keyword = nil
end

Public Instance Methods

after_background(_background) click to toggle source
# File lib/cucumber/formatter/html.rb, line 140
def after_background(_background)
  @in_background = nil
  builder << '</div>'
end
after_comment(_comment) click to toggle source
# File lib/cucumber/formatter/html.rb, line 98
def after_comment(_comment)
  builder << '</pre>'
end
after_examples(_examples) click to toggle source
# File lib/cucumber/formatter/html.rb, line 205
def after_examples(_examples)
  builder << '</div>'
end
after_feature(_feature) click to toggle source
# File lib/cucumber/formatter/html.rb, line 90
def after_feature(_feature)
  builder << '</div>'
end
after_feature_element(_feature_element) click to toggle source
# File lib/cucumber/formatter/html.rb, line 162
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 78
def after_features(features)
  print_stats(features)
  builder << '</div>'
  builder << '</body>'
  builder << '</html>'
end
after_multiline_arg(multiline_arg) click to toggle source
# File lib/cucumber/formatter/html.rb, line 302
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 195
def after_outline_table(_outline_table)
  builder << '</table>'
  @outline_row = nil
  @inside_outline = false
end
after_step(_step) click to toggle source
# File lib/cucumber/formatter/html.rb, line 233
def after_step(_step)
  move_progress
end
after_step_result(keyword, step_match, _multiline_arg, status, _exception, _source_indent, _background, _file_colon_line) click to toggle source
# File lib/cucumber/formatter/html.rb, line 257
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 221
def after_steps(_steps)
  print_messages
  builder << '</ol>' if @in_background || @in_scenario_outline
end
after_table_row(table_row) click to toggle source
# File lib/cucumber/formatter/html.rb, line 323
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 107
def after_tags(_tags)
  @tag_spacer = nil
end
after_test_case(_test_case, result) click to toggle source
# File lib/cucumber/formatter/html.rb, line 387
def after_test_case(_test_case, result)
  if result.failed? && !@scenario_red
    set_scenario_color_failed
  end
end
background_name(keyword, name, _file_colon_line, _source_indent) click to toggle source
# File lib/cucumber/formatter/html.rb, line 145
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 135
def before_background(_background)
  @in_background = true
  builder << '<div class="background">'
end
before_comment(_comment) click to toggle source
# File lib/cucumber/formatter/html.rb, line 94
def before_comment(_comment)
  builder << '<pre class="comment">'
end
before_examples(_examples) click to toggle source
# File lib/cucumber/formatter/html.rb, line 201
def before_examples(_examples)
  builder << '<div class="examples">'
end
before_feature(_feature) click to toggle source
# File lib/cucumber/formatter/html.rb, line 85
def before_feature(_feature)
  @exceptions = []
  builder << '<div class="feature">'
end
before_feature_element(feature_element) click to toggle source
# File lib/cucumber/formatter/html.rb, line 154
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 71
def before_features(features)
  @step_count = features && features.step_count || 0 # TODO: Make this work with core!

  builder.build_document!
  builder.format_features! features
end
before_multiline_arg(multiline_arg) click to toggle source
# File lib/cucumber/formatter/html.rb, line 295
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 189
def before_outline_table(_outline_table)
  @inside_outline = true
  @outline_row = 0
  builder << '<table>'
end
before_step(step) click to toggle source
# File lib/cucumber/formatter/html.rb, line 226
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 237
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
  scenario_color(status)
  builder << "<li id='#{@step_id}' class='step #{status}'>"
end
before_steps(_steps) click to toggle source
# File lib/cucumber/formatter/html.rb, line 217
def before_steps(_steps)
  builder << '<ol>'
end
before_table_row(table_row) click to toggle source
# File lib/cucumber/formatter/html.rb, line 316
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 131
def before_test_case(_test_case)
  @previous_step_keyword = nil
end
comment_line(comment_line) click to toggle source
# File lib/cucumber/formatter/html.rb, line 102
def comment_line(comment_line)
  builder.text!(comment_line)
  builder.br
end
doc_string(string) click to toggle source
# File lib/cucumber/formatter/html.rb, line 309
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 41
def embed(src, mime_type, label)
  if image?(mime_type)
    src = src_is_file_or_data?(src) ? src : "data:#{standardize_mime_type(mime_type)},#{src}"
    builder.embed(type: :image, src: path(src), label: label, id: next_id(:img))
  else
    builder.embed(type: :text, src: src, label: label, id: next_id(:text))
  end
end
empty_messages() click to toggle source
# File lib/cucumber/formatter/html.rb, line 383
def empty_messages
  @delayed_messages = []
end
examples_name(keyword, name) click to toggle source
# File lib/cucumber/formatter/html.rb, line 209
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 284
def exception(exception, _status)
  return if @hide_this_step
  print_messages
  build_exception_detail(exception)
end
extra_failure_content(file_colon_line) click to toggle source
# File lib/cucumber/formatter/html.rb, line 290
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 117
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
image?(mime_type) click to toggle source
# File lib/cucumber/formatter/html.rb, line 67
def image?(mime_type)
  mime_type =~ /^image\/(png|gif|jpg|jpeg)/
end
path(src) click to toggle source
# File lib/cucumber/formatter/html.rb, line 50
def path(src)
  if @io.respond_to?(:path) && 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

  src
end
print_messages() click to toggle source
print_table_row_messages() click to toggle source
puts(message) click to toggle source
# File lib/cucumber/formatter/html.rb, line 359
def puts(message)
  @delayed_messages << message
end
scenario_name(keyword, name, file_colon_line, _source_indent) click to toggle source
# File lib/cucumber/formatter/html.rb, line 171
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
src_is_file_or_data?(src) click to toggle source
# File lib/cucumber/formatter/html.rb, line 63
def src_is_file_or_data?(src)
  File.file?(src) || src =~ /^data:image\/(png|gif|jpg|jpeg);base64,/
end
standardize_mime_type(mime_type) click to toggle source
# File lib/cucumber/formatter/html.rb, line 59
def standardize_mime_type(mime_type)
  mime_type =~ /;base[0-9]+$/ ? mime_type : mime_type + ';base64'
end
step_name(keyword, step_match, status, _source_indent, background, _file_colon_line) click to toggle source
# File lib/cucumber/formatter/html.rb, line 275
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 348
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)
  scenario_color(status) if @inside_outline
  @col_index += 1
end
tag_name(tag_name) click to toggle source
# File lib/cucumber/formatter/html.rb, line 111
def tag_name(tag_name)
  builder.text!(@tag_spacer) if @tag_spacer
  @tag_spacer = ' '
  builder.span(tag_name, :class => 'tag')
end

Protected Instance Methods

backtrace_line(line) click to toggle source
# File lib/cucumber/formatter/html.rb, line 512
def backtrace_line(line)
  if ENV['TM_PROJECT_DIRECTORY']
    line.gsub(/^([^:]*\.(?:rb|feature|haml)):(\d*).*$/) do
      "<a href=\"txmt://open?url=file://#{File.expand_path($1)}&line=#{$2}\">#{$1}:#{$2}</a> "
    end
  else
    line
  end
end
build_cell(cell_type, value, attributes) click to toggle source
# File lib/cucumber/formatter/html.rb, line 488
def build_cell(cell_type, value, attributes)
  builder.__send__(cell_type, attributes) do
    builder.div do
      builder.span(value, :class => 'step param')
    end
  end
end
build_exception_detail(exception) click to toggle source
# File lib/cucumber/formatter/html.rb, line 401
def build_exception_detail(exception)
  backtrace = Array.new

  builder.div(:class => 'message') do
    message = exception.message

    if defined?(RAILS_ROOT) && message.include?('Exception caught')
      matches = message.match(/Showing <i>(.+)<\/i>(?:.+) #(\d+)/)
      backtrace += ["#{RAILS_ROOT}/#{matches[1]}:#{matches[2]}"] if matches
      matches = message.match(/<code>([^(\/)]+)<\//m)
      message = matches ? matches[1] : ''
    end

    unless exception.instance_of?(RuntimeError)
      message = "#{message} (#{exception.class})"
    end

    builder.pre do
      builder.text!(message)
    end
  end

  builder.div(:class => 'backtrace') do
    builder.pre do
      backtrace = exception.backtrace
      backtrace.delete_if { |x| x =~ /\/gems\/(cucumber|rspec)/ }
      builder << backtrace_line(backtrace.join("\n"))
    end
  end

  extra = extra_failure_content(backtrace)
  builder << extra unless extra == ''
end
build_step(keyword, step_match, _status) click to toggle source
# File lib/cucumber/formatter/html.rb, line 465
def build_step(keyword, step_match, _status)
  step_name = step_match.format_args(lambda { |param| %{<span class="param">#{param}</span>} })
  builder.div(:class => 'step_name') do |div|
    builder.span(keyword, :class => 'keyword')
    builder.span(:class => 'step val') do |name|
      name << h(step_name).gsub(/&lt;span class=&quot;(.*?)&quot;&gt;/, '<span class="\1">').gsub(/&lt;\/span&gt;/, '</span>')
    end
  end

  step_file = step_match.file_colon_line
  step_file.gsub(/^([^:]*\.rb):(\d*)/) do
    if ENV['TM_PROJECT_DIRECTORY']
      step_file = "<a href=\"txmt://open?url=file://#{File.expand_path($1)}&line=#{$2}\">#{$1}:#{$2}</a> "
    end
  end

  builder.div(:class => 'step_file') do |div|
    builder.span do
      builder << step_file
    end
  end
end
dump_count(count, what, state = nil) click to toggle source
# File lib/cucumber/formatter/html.rb, line 546
def dump_count(count, what, state = nil)
  [count, state, "#{what}#{count == 1 ? '' : 's'}"].compact.join(' ')
end
format_exception(exception) click to toggle source
# File lib/cucumber/formatter/html.rb, line 508
def format_exception(exception)
  ([exception.message.to_s] + exception.backtrace).join("\n")
end
move_progress() click to toggle source
# File lib/cucumber/formatter/html.rb, line 496
def move_progress
  builder << " <script type=\"text/javascript\">moveProgressBar('#{percent_done}');</script>"
end
next_id(type) click to toggle source
# File lib/cucumber/formatter/html.rb, line 395
def next_id(type)
  @indices ||= Hash.new { 0 }
  @indices[type] += 1
  "#{type}_#{@indices[type]}"
end
outline_step?(_step) click to toggle source
# File lib/cucumber/formatter/html.rb, line 550
def outline_step?(_step)
  not @step.step.respond_to?(:actual_keyword)
end
percent_done() click to toggle source
# File lib/cucumber/formatter/html.rb, line 500
def percent_done
  result = 100.0
  if @step_count != 0
    result = ((@step_number).to_f / @step_count.to_f * 1000).to_i / 10.0
  end
  result
end
print_stat_string(_features) click to toggle source
print_stats(features) click to toggle source
print_status_counts() { |status| ... } click to toggle source
scenario_color(status) click to toggle source
# File lib/cucumber/formatter/html.rb, line 435
def scenario_color(status)
  if status.nil? || status == :undefined || status == :pending
    set_scenario_color_pending
  end
  if status == :failed
    set_scenario_color_failed
  end
end
set_scenario_color_failed() click to toggle source
# File lib/cucumber/formatter/html.rb, line 444
def set_scenario_color_failed
  builder.script do
    builder.text!("makeRed('cucumber-header');") unless @header_red
    @header_red = true
    scenario_or_background = @in_background ? 'background' : 'scenario'
    builder.text!("makeRed('#{scenario_or_background}_#{@scenario_number}');") unless @scenario_red
    @scenario_red = true
    if @options[:expand] && @inside_outline
      builder.text!("makeRed('#{scenario_or_background}_#{@scenario_number}_#{@outline_row}');")
    end
  end
end
set_scenario_color_pending() click to toggle source
# File lib/cucumber/formatter/html.rb, line 457
def set_scenario_color_pending
  builder.script do
    builder.text!("makeYellow('cucumber-header');") unless @header_red
    scenario_or_background = @in_background ? 'background' : 'scenario'
    builder.text!("makeYellow('#{scenario_or_background}_#{@scenario_number}');") unless @scenario_red
  end
end