textarcana (owner)

Revisions

gist: 139081 Download_button fork
public
Public Clone URL: git://gist.github.com/139081.git
Embed All Files: show embed
screenshot.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env ruby
# :title:Selenium Screenshot Helper
# = Name
# Selenium Screenshot Helper
#
# = Description
#
# Simply requiring this file in a Test::Unit test case, will cause
# every call to "assert" to capture a screenshot. No configuration is
# required other than including this module.
#
# Note that only the plain "assert" method is overridden (eg,
# screenshots will not be captured if you use assert_not,
# assert_equal, etc).
 
require "rubygems"
require "test/unit"
require "selenium/client"
 
module ScreenshotHelper
 
 
  # == Save a screenshot to the log/png/ directory
  #
  # Screenshots are saved in log/png/ and a long entry noting the
  # screenshot was taken, is written to log/test.log. Both of those
  # paths are relative to the execution path.
  #
  # Note that this script does not create the log/ nor log/png/
  # directories, and an error will be thrown unless they exist.
  #
  # File names will be a time-stamp plus the url of the page, less the
  # protocol, with dots and slashes replaced by underscores, and all
  # other alphanumerics removed.
  #
  # === IMPORTANT: Screenshots and proxyInjection mode
  #
  # @selenium.capture_entire_page_screenshot is not supported in proxy
  # injection mode. In that case I have to use the much inferior
  # capture_screenshot, which WILL NOT WORK if Firefox is in the
  # background when the test runs.
  #
  # That is, when taking screenshots in proxy injection mode, the test
  # suite cannot run in the background.
 
  def screenshot
 
    working_directory = File.expand_path($0).gsub /#{Regexp.escape $0}/, ''
 
    if (PLATFORM.match /cygwin/)
      working_directory = working_directory.gsub %r{/cygdrive/c/}, 'c:/'
    end
 
    url_as_filename = @selenium.location.gsub(%r{http.*://}, "").gsub(%r{[/.]}, "_").gsub(/[^A-Za-z0-9_.]/, "")
 
    method_name = self.name.gsub /\(.*/, ''
 
    klass_name = self.name.match(/\((.*)\)/)[1]
 
    # TODO print the assertion count instead of the file name
 
    filepath = "#{working_directory}log/png/#{klass_name}__#{method_name}__#{url_as_filename}__#{Time.now.to_i}.png"
 
    if (@selenium.browser_string.match /\*pifirefox/)
 
      @selenium.window_maximize
      @selenium.capture_screenshot(filepath)
 
    else
 
      warn "working directory is #{working_directory}}"
 
      @selenium.capture_entire_page_screenshot(filepath, "")
 
    end
 
    # TODO log entry should be a subroutine
 
    method_identifier = "#{method_name}_#{klass_name}"
 
    log = File.open "#{working_directory}log/test.log", "a"
 
    log.write "#{Time.now.utc.localtime}: #{self.name}: screenshot: #{filepath}"
 
  end
 
end
 
 
# == Assertions that take screenshots
#
# Override assert method of Test::Unit to add capturing screenshot,
# so that a screenshot is automatically captured just before any
# assert is made.
 
class Test::Unit::TestCase
 
  include ScreenshotHelper
 
  define_method(:assert) do
 
    screenshot
 
    super
 
  end
 
end