Class: WatiRspec::HtmlFormatter

Inherits:
Spec::Runner::Formatter::HtmlFormatter
  • Object
show all
Defined in:
lib/watirspec/html_formatter.rb

Overview

Custom RSpec formatter for WatiRspec

  • 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(options, output) ⇒ HtmlFormatter

:nodoc:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/watirspec/html_formatter.rb', line 19

def initialize(options, output) # :nodoc:
  raise "output has to be a file path!" unless output.is_a?(String)
  @output_dir = File.expand_path(File.dirname(output))
  puts "Results will be saved into the directory #{@output_dir}"
  @files_dir = File.join(@output_dir, "files")
  if File.exists?(@output_dir)
    archive_dir = File.join(@output_dir, "../archive")
    FileUtils.mkdir_p(archive_dir) unless File.exists?(archive_dir)
    FileUtils.mv @output_dir, File.join(archive_dir, "#{File.basename(@output_dir)}_#{File.mtime(@output_dir).strftime("%y%m%d_%H%M%S")}")
  end
  FileUtils.mkdir_p(@files_dir)
  @files_saved_during_example = []
  super
end

Instance Attribute Details

#browser=(value) ⇒ Object (writeonly)

currently used browser object needed for saving of screenshots and html



17
18
19
# File 'lib/watirspec/html_formatter.rb', line 17

def browser=(value)
  @browser = value
end

Instance Method Details

#example_started(example) ⇒ Object

:nodoc:



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

def example_started(example) # :nodoc:
  @files_saved_during_example.clear
  super
end

#extra_failure_content(failure) ⇒ Object

:nodoc:



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

def extra_failure_content(failure) # :nodoc:
  save_javascript_error
  save_html
  save_screenshot

  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.



109
110
111
112
113
114
115
# File 'lib/watirspec/html_formatter.rb', line 109

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:



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

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_dir))}'>#{description}</a>&nbsp;"
end

#save_htmlObject

:nodoc:



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

def save_html # :nodoc:
  begin
    html = @browser.html
    file_name = file_path("browser.html")
    File.open(file_name, 'w') {|f| f.puts html}
  rescue => e
    $stderr.puts "saving of html failed: #{e.message}"
    $stderr.puts e.backtrace
  end
  file_name
end

#save_javascript_errorObject

:nodoc:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/watirspec/html_formatter.rb', line 86

def save_javascript_error # :nodoc:
  file_name = nil
  begin
    if @browser.is_a?(Watir::IE) && @browser.status =~ /Error on page/
      autoit = Watir::autoit
      autoit.AutoItSetOption("MouseCoordMode", 0)
      autoit.ControlClick("[TITLE:#{@browser.title}]", "", "[CLASS:msctls_statusbar32]", "left", 2)
      popup_title = "[REGEXPTITLE:^(Windows )?Internet Explorer$]"
      autoit.WinWait(popup_title, "", 10)
      file_name = save_screenshot("JS_Error", autoit.WinGetHandle(popup_title).hex)
      autoit.WinClose(popup_title)
    end
  rescue => e
    $stderr.puts "saving of javascript error failed: #{e.message}"
    $stderr.puts e.backtrace
  end
  file_name
end

#save_screenshot(description = "Screenshot", hwnd = @browser.hwnd) ⇒ Object

:nodoc:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/watirspec/html_formatter.rb', line 71

def save_screenshot(description="Screenshot", hwnd=@browser.hwnd) # :nodoc:
  begin
    @browser.bring_to_front
    width, height, blob = Win32::Screenshot.capture_hwnd(hwnd)
    file_name = file_path("screenshot.png", description)
    img = Magick::ImageList.new
    img.from_blob(blob)
    img.write(file_name)
  rescue => e
    $stderr.puts "saving of screenshot failed: #{e.message}"
    $stderr.puts e.backtrace
  end
  file_name
end