Class: WatirCats::Snapper

Inherits:
Object
  • Object
show all
Defined in:
lib/watircats/snapper.rb

Constant Summary collapse

BROWSER_HEIGHT =
5000

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, site_map) ⇒ Snapper

Returns a new instance of Snapper.



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
# File 'lib/watircats/snapper.rb', line 8

def initialize(base_url, site_map)
  @base_url       = base_url
  @screenshot_dir = WatirCats.config.screenshot_dir
  @widths         = WatirCats.config.widths
  @images_dir     = WatirCats.config.images_dir
  
  # Handle the environments that require profile configuration
  configure_browser

  # Allowing for custom page class tests
  @class_tests          = [ ]
  @class_test_mapping   = { }
  @@custom_test_results = { }

  if WatirCats.config.custom_body_class_tests
    load WatirCats.config.custom_body_class_tests
    $custom_body_class_tests.each do |custom_test|
      # Store the custom class tests in the class_tests array
      # Map the class to the relevant method name
      
      body_class = custom_test[:body_class_name]
      @class_tests << body_class
      if @class_test_mapping[ body_class ] 
        @class_test_mapping[ body_class ] = @class_test_mapping[ body_class ] + [ custom_test[:method_name] ]
      else 
        @class_test_mapping[ body_class ] = [ custom_test[:method_name] ]
      end

      custom_code = "def #{custom_test[:method_name]}; #{custom_test[:custom_code]}; end"
      WatirCats::Snapper.class_eval custom_code
    end
  end

  # Retrieve the paths from the sitemap
  @paths      = site_map.the_paths
  @time_stamp = Time.now.to_i.to_s

  process_paths
  @browser.quit
end

Class Method Details

.custom_test_resultsObject



166
167
168
# File 'lib/watircats/snapper.rb', line 166

def self.custom_test_results
  @@custom_test_results ||= { }
end

.foldersObject



78
79
80
81
# File 'lib/watircats/snapper.rb', line 78

def self.folders
  # Class variable, to keep track of folders amongst all instances of this class
  @@folders ||= []
end

Instance Method Details

#add_folder(folder) ⇒ Object



83
84
85
86
# File 'lib/watircats/snapper.rb', line 83

def add_folder(folder)
  @@folders ||= [] # Ensure @@folders exists
  @@folders << folder
end

#capture_page_image(url, file_name) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/watircats/snapper.rb', line 54

def capture_page_image(url, file_name)
  
  # skip existing screenshots if we've specified that option
  if WatirCats.config.skip_existing
    if FileTest.exists?(file_name)
     puts "Skipping existing file at " + file_name
     return
    end
  end


  @browser.goto url
  # Wait for page to complete loading by querying document.readyState
  script = "return document.readyState"
  @browser.wait_until { @browser.execute_script(script) == "complete" }
  
  # Take and save the screenshot      
  @browser.screenshot.save(file_name)
end

#configure_browserObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/watircats/snapper.rb', line 141

def configure_browser
  engine = WatirCats.config.browser || :ff

  # Firefox only stuff, allowing a custom binary location and proxy support
  if ( engine.to_sym == :ff || engine.to_sym == :firefox )

    bin_path = WatirCats.config.ff_path
    proxy    = WatirCats.config.proxy
    ::Selenium::WebDriver::Firefox::Binary.path= bin_path if bin_path.is_a? String
    
    profile = ::Selenium::WebDriver::Firefox::Profile.new 
    
    if proxy
      profile.proxy = ::Selenium::WebDriver::Proxy.new :http => proxy, :ssl => proxy
    end

    profile['app.update.auto']    = false
    profile['app.update.enabled'] = false

    @browser = ::Watir::Browser.new engine, :profile => profile
  else
    @browser = ::Watir::Browser.new engine
  end
end

#process_pathsObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/watircats/snapper.rb', line 88

def process_paths
  # Build the unique directory unless it exists
  FileUtils.mkdir "#{@screenshot_dir}" unless File.directory? "#{@screenshot_dir}"

  stamped_base_url_folder = "#{@screenshot_dir}/#{@base_url}-#{@time_stamp}"
  if @images_dir
      stamped_base_url_folder = "#{@screenshot_dir}/#{@images_dir}"
  end
  
  FileUtils.mkdir "#{stamped_base_url_folder}" unless File.directory? "#{stamped_base_url_folder}"
 
  add_folder(stamped_base_url_folder)

  # Some setup for processing
  paths = @paths
  widths = self.widths

  # Iterate through the paths, using the key as a label, value as a path
  paths.each do |label, path|
    # Create our base array to use to execute tests
    potential_body_classes = [:all]
    # Do custom tests here
    body_classes = @browser.body.class_name
    # Split the class string for the <body> element on spaces, then shovel
    # each body_class into the potential_body_classes array
    body_classes.split.each { |body_class| potential_body_classes << body_class }
    
    @@custom_test_results[path] = {}

    potential_body_classes.each do |the_class|
      if @class_tests.include? the_class
        methods_to_send = @class_test_mapping[the_class]

        methods_to_send.each do |custom_method|
          @@custom_test_results[path][custom_method] = self.send( custom_method )
        end
      
      end
    end

    # Skip if a redirect matches the avoided path
    if WatirCats.config.avoided_path
      next if @browser.url.match( /#{WatirCats.config.avoided_path}/ )
    end
    # For each width, resize the browser, take a screenshot
    widths.each do |width|
      resize_browser width
      file_name = "#{stamped_base_url_folder}/#{label}_#{width}.png"
      capture_page_image("#{@base_url}#{path}", file_name)
    end
  end
end

#resize_browser(width) ⇒ Object



49
50
51
52
# File 'lib/watircats/snapper.rb', line 49

def resize_browser(width)
  # Set a max height using the constant BROWSER_HEIGHT
  @browser.window.resize_to(width, BROWSER_HEIGHT)
end

#widthsObject



74
75
76
# File 'lib/watircats/snapper.rb', line 74

def widths
  @widths = @widths || [1024]
end