Class: WatirSplash::HtmlFormatter

Inherits:
RSpec::Core::Formatters::HtmlFormatter
  • Object
show all
Defined in:
lib/watirsplash/html_formatter.rb

Overview

Custom RSpec formatter for WatirSplash

  • saves screenshot of the browser upon test failure

  • saves html of the browser upon test failure

  • saves javascript error dialog upon test failure

  • saves all files generated/downloaded during the test and shows them in the report

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ HtmlFormatter

:nodoc:



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/watirsplash/html_formatter.rb', line 15

def initialize(output) # :nodoc:

  @output_path = File.expand_path(ENV["WATIRSPLASH_RESULTS_PATH"] || (output.respond_to?(:path) ? output.path : "results/index.html"))
  archive_results 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.join(File.dirname(@output_path), "files")
  FileUtils.mkdir_p(@files_dir)
  @files_saved_during_example = []
  super(File.open(@output_path, "w"))
end

Instance Attribute Details

#output_relative_pathObject (readonly)

Returns the value of attribute output_relative_path.



13
14
15
# File 'lib/watirsplash/html_formatter.rb', line 13

def output_relative_path
  @output_relative_path
end

Instance Method Details

#example_group_started(example_group) ⇒ Object

:nodoc:



27
28
29
30
31
# File 'lib/watirsplash/html_formatter.rb', line 27

def example_group_started(example_group) # :nodoc:

  @files_saved_during_example.clear
  append_extra_information_to_description(example_group)
  super
end

#example_started(example) ⇒ Object

:nodoc:



33
34
35
36
37
# File 'lib/watirsplash/html_formatter.rb', line 33

def example_started(example) # :nodoc:

  @files_saved_during_example.clear
  example.[:description] += "#{example.metadata[:location].scan(/:\d+$/).flatten.first} (#{Time.now.strftime("%H:%M:%S")})"
  super
end

#extra_failure_content(exception) ⇒ Object

:nodoc:



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

def extra_failure_content(exception) # :nodoc:

  if WatirSplash::Browser.exists?
    save_screenshot
    save_html
  end

  content = []
  content << "<span>"
  @files_saved_during_example.each {|f| content << link_for(f)}
  content << "</span>"
  super + content.join($/)
end

#file_path(file_name, description = nil) ⇒ Object

Generates unique file name and path for each example.

All file names generated with this method will be shown on the report.



81
82
83
84
85
86
87
# File 'lib/watirsplash/html_formatter.rb', line 81

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

:nodoc:



52
53
54
55
56
57
58
# File 'lib/watirsplash/html_formatter.rb', line 52

def link_for(file) # :nodoc:

  return unless File.exists?(file[:path])

  description = file[:desc] ? file[:desc] : File.extname(file[:path]).upcase[1..-1]
  path = Pathname.new(file[:path])
  "<a href='#{path.relative_path_from(Pathname.new(@output_path).dirname)}'>#{description}</a>&nbsp;"
end

#save_htmlObject

:nodoc:



60
61
62
63
64
65
66
67
68
69
# File 'lib/watirsplash/html_formatter.rb', line 60

def save_html # :nodoc:

  file_name = file_path("browser.html")
  begin
    html = WatirSplash::Browser.current.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(description = "Screenshot", hwnd = nil) ⇒ Object

:nodoc:



71
72
73
74
75
# File 'lib/watirsplash/html_formatter.rb', line 71

def save_screenshot(description="Screenshot", hwnd=nil) # :nodoc:

  file_name = file_path("screenshot.png", description)
  WatirSplash::Browser.current.save_screenshot(:file_name => file_name, :hwnd => hwnd)
  file_name
end