Parent

Class/Module Index [+]

Quicksearch

Cucumber::Formatter::Html

Public Class Methods

new(step_mother, path_or_io, options) click to toggle source
# 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
end

Public Instance Methods

after_background(background) click to toggle source
# File lib/cucumber/formatter/html.rb, line 132
def after_background(background)
  @in_background = nil
  @builder << '</div>'
end
after_comment(comment) click to toggle source
# File lib/cucumber/formatter/html.rb, line 94
def after_comment(comment)
  @builder << '</pre>'
end
after_examples(examples) click to toggle source
# File lib/cucumber/formatter/html.rb, line 184
def after_examples(examples)
  @builder << '</div>'
end
after_feature(feature) click to toggle source
# File lib/cucumber/formatter/html.rb, line 86
def after_feature(feature)
  @builder << '</div>'
end
after_feature_element(feature_element) click to toggle source
# File lib/cucumber/formatter/html.rb, line 156
def after_feature_element(feature_element)
  @builder << '</div>'
  @open_step_list = true
end
after_features(features) click to toggle source
# File lib/cucumber/formatter/html.rb, line 74
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 273
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 175
def after_outline_table(outline_table)
  @builder << '</table>'
  @outline_row = nil
end
after_step(step) click to toggle source
# File lib/cucumber/formatter/html.rb, line 210
def after_step(step)
  move_progress
end
after_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background) click to toggle source
# File lib/cucumber/formatter/html.rb, line 234
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>'
end
after_steps(steps) click to toggle source
# File lib/cucumber/formatter/html.rb, line 200
def after_steps(steps)
  @builder << '</ol>'
end
after_table_row(table_row) click to toggle source
# File lib/cucumber/formatter/html.rb, line 295
def after_table_row(table_row)
  return if @hide_this_step
  @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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 103
def after_tags(tags)
  @tag_spacer = nil
end
announce(announcement) click to toggle source
# File lib/cucumber/formatter/html.rb, line 326
def announce(announcement)
  @builder.pre(announcement, :class => 'announcement')
end
background_name(keyword, name, file_colon_line, source_indent) click to toggle source
# File lib/cucumber/formatter/html.rb, line 137
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 127
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 90
def before_comment(comment)
  @builder << '<pre class="comment">'
end
before_examples(examples) click to toggle source
# File lib/cucumber/formatter/html.rb, line 180
def before_examples(examples)
   @builder << '<div class="examples">'
end
before_feature(feature) click to toggle source
# File lib/cucumber/formatter/html.rb, line 81
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 146
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 41
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')
    end
  end
end
before_multiline_arg(multiline_arg) click to toggle source
# File lib/cucumber/formatter/html.rb, line 266
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 170
def before_outline_table(outline_table)
  @outline_row = 0
  @builder << '<table>'
end
before_step(step) click to toggle source
# File lib/cucumber/formatter/html.rb, line 204
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 214
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 196
def before_steps(steps)
  @builder << '<ol>'
end
before_table_row(table_row) click to toggle source
# File lib/cucumber/formatter/html.rb, line 288
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 98
def comment_line(comment_line)
  @builder.text!(comment_line)
  @builder.br
end
embed(file, mime_type) click to toggle source
# File lib/cucumber/formatter/html.rb, line 25
def embed(file, mime_type)
  case(mime_type)
  when /^image\/(png|gif|jpg|jpeg)/
    embed_image(file)
  end
end
embed_image(file) click to toggle source
# File lib/cucumber/formatter/html.rb, line 32
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
examples_name(keyword, name) click to toggle source
# File lib/cucumber/formatter/html.rb, line 188
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 257
def exception(exception, status)
  build_exception_detail(exception)
end
extra_failure_content(file_colon_line) click to toggle source
# File lib/cucumber/formatter/html.rb, line 261
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 113
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
py_string(string) click to toggle source
# File lib/cucumber/formatter/html.rb, line 280
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 161
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 246
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 315
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) click to toggle source
# File lib/cucumber/formatter/html.rb, line 107
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 496
def backtrace_line(line)
  line.gsub(/^([^:]*\.(?:rb|feature|haml)):(\d*)/) do
    if ENV['TM_PROJECT_DIRECTORY']
      "<a href=\"txmt://open?url=file://#{File.expand_path($1)}&line=#{$2}\">#{$1}:#{$2}</a> "
    else
      line
    end
  end
end
build_cell(cell_type, value, attributes) click to toggle source
# File lib/cucumber/formatter/html.rb, line 443
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 332
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]}"]
      message = message.match(/<code>([^(\/)]+)<\//)[1]
    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 420
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
create_builder(io) click to toggle source
# File lib/cucumber/formatter/html.rb, line 534
def create_builder(io)
  OrderedXmlMarkup.new(:target => io, :indent => 0)
end
dump_count(count, what, state=nil) click to toggle source
# File lib/cucumber/formatter/html.rb, line 530
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 492
def format_exception(exception)
  (["#{exception.message}"] + exception.backtrace).join("\n")
end
get_step_count(features) click to toggle source
# File lib/cucumber/formatter/html.rb, line 381
def get_step_count(features)
  count = 0
  features = features.instance_variable_get("@features")
  features.each do |feature|
    #get background steps
    if feature.instance_variable_get("@background")
      background = feature.instance_variable_get("@background")
      background.init
      background_steps = background.instance_variable_get("@steps").instance_variable_get("@steps")
      count += background_steps.size
    end
    #get scenarios
    feature.instance_variable_get("@feature_elements").each do |scenario|
      scenario.init
      #get steps
      steps = scenario.instance_variable_get("@steps").instance_variable_get("@steps")
      count += steps.size

      #get example table
      examples = scenario.instance_variable_get("@examples_array")
      unless examples.nil?
        examples.each do |example|
          example_matrix = example.instance_variable_get("@outline_table").instance_variable_get("@cell_matrix")
          count += example_matrix.size
        end
      end

      #get multiline step tables
      steps.each do |step|
        multi_arg = step.instance_variable_get("@multiline_arg")
        next if multi_arg.nil?
        matrix = multi_arg.instance_variable_get("@cell_matrix")
        count += matrix.size unless matrix.nil?
      end
    end
  end
  return count
end
inline_css() click to toggle source
# File lib/cucumber/formatter/html.rb, line 451
def inline_css
  @builder.style(:type => 'text/css') do
    @builder << File.read(File.dirname(__FILE__) + '/cucumber.css')
  end
end
inline_js() click to toggle source
# File lib/cucumber/formatter/html.rb, line 457
def inline_js
  @builder.script(:type => 'text/javascript') do
    @builder << inline_js_content
  end
end
inline_js_content() click to toggle source
# File lib/cucumber/formatter/html.rb, line 463
def inline_js_content
        function moveProgressBar(percentDone) {      document.getElementById("cucumber-header").style.width = percentDone +"%";      }      function makeRed(element_id) {      document.getElementById(element_id).style.background = '#C40D0D';      document.getElementById(element_id).style.color = '#FFFFFF';      }      function makeYellow(element_id) {      document.getElementById(element_id).style.background = '#FAF834';      document.getElementById(element_id).style.color = '#000000';      }
end
move_progress() click to toggle source
# File lib/cucumber/formatter/html.rb, line 480
def move_progress
  @builder << " <script type=\"text/javascript\">moveProgressBar('#{percent_done}');</script>"
end
percent_done() click to toggle source
# File lib/cucumber/formatter/html.rb, line 484
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
set_scenario_color(status) click to toggle source
# File lib/cucumber/formatter/html.rb, line 356
def set_scenario_color(status)
  if status == :undefined
    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 365
def set_scenario_color_failed
  @builder.script do
    @builder.text!("makeRed('cucumber-header');") unless @header_red
    @header_red = true
    @builder.text!("makeRed('scenario_#{@scenario_number}');") unless @scenario_red
    @scenario_red = true
  end
end
set_scenario_color_pending() click to toggle source
# File lib/cucumber/formatter/html.rb, line 374
def set_scenario_color_pending
  @builder.script do
    @builder.text!("makeYellow('cucumber-header');") unless @header_red
    @builder.text!("makeYellow('scenario_#{@scenario_number}');") unless @scenario_red
  end         
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.