class Watir::RSpec::HtmlFormatter

Custom RSpec formatter

Public Class Methods

new(output) click to toggle source

@private

Calls superclass method
# File lib/watir/rspec/html_formatter.rb, line 17
def initialize(output)
  @output_path = File.expand_path(ENV["WATIR_RESULTS_PATH"] || (output.respond_to?(:path) ? output.path : "tmp/spec-results/index.html"))
  FileUtils.rm_rf File.dirname(@output_path), :verbose => true if File.exists?(@output_path)

  @output_relative_path = Pathname.new(@output_path).relative_path_from(Pathname.new(Dir.pwd))
  puts "Results will be saved to #{@output_relative_path}"

  @files_dir = File.dirname(@output_path)
  FileUtils.mkdir_p(@files_dir)
  @files_saved_during_example = []

  super(File.open(@output_path, "w"))
end

Public Instance Methods

example_group_started(example_group) click to toggle source

@private

Calls superclass method
# File lib/watir/rspec/html_formatter.rb, line 32
def example_group_started(example_group)
  @files_saved_during_example.clear
  super
end
example_started(example) click to toggle source

@private

Calls superclass method
# File lib/watir/rspec/html_formatter.rb, line 38
def example_started(example)
  @files_saved_during_example.clear
  super
end
file_path(file_name, description=nil) click to toggle source

Generate unique file path for the current spec. If the file will be created during that spec and spec fails then it will be shown automatically in the html report.

@param [String] file_name File name to be used for file.

Will be used as a part of the complete name.

@return [String] Absolute path for the unique file name.

# File lib/watir/rspec/html_formatter.rb, line 50
def file_path(file_name, description=nil)
  extension = File.extname(file_name)
  basename = File.basename(file_name, extension)
  file_path = File.join(@files_dir, "#{basename}_#{::Time.now.strftime("%H%M%S")}_#{example_group_number}_#{example_number}#{extension}")
  @files_saved_during_example.unshift(:desc => description, :path => file_path)
  file_path
end

Private Instance Methods

extra_failure_content(exception) click to toggle source

@private

Calls superclass method
# File lib/watir/rspec/html_formatter.rb, line 61
def extra_failure_content(exception)
  return super unless example_group  # apparently there are cases where rspec failures are encountered and the example_group is not set (i.e. nil)
  browser = example_group.before_context_ivars[:@browser] || $browser
  return super unless browser && browser.exists?

  save_screenshot browser
  save_html browser

  content = []
  content << "<span>"
  @files_saved_during_example.each {|f| content << link_for(f)}
  content << "</span>"
  super + content.join($/)
end
save_html(browser) click to toggle source
# File lib/watir/rspec/html_formatter.rb, line 84
def save_html(browser)
  file_name = file_path("browser.html")
  begin
    html = browser.html
    File.open(file_name, 'w') {|f| f.puts html}
  rescue => e
    $stderr.puts "saving of html failed: #{e.message}"
  end
  file_name
end
save_screenshot(browser, description="Screenshot") click to toggle source
# File lib/watir/rspec/html_formatter.rb, line 95
def save_screenshot(browser, description="Screenshot")
  file_name = file_path("screenshot.png", description)
  browser.screenshot.save(file_name)
  file_name
end