class Capybara::Paparazzi::Shooter

Constants

CONFIG_VARS
DEFAULT_FOLD_STYLE
DEFAULT_SCREENSHOT_SIZES

Attributes

do_shot[RW]
driver[RW]
event_details[RW]
filename[RW]
path[RW]
screenshot_size[RW]
suffix[RW]

Public Class Methods

new(driver, event_details=nil) click to toggle source
# File lib/capybara/paparazzi/shooter.rb, line 144
def initialize(driver, event_details=nil)
  @driver = driver
  @event_details = event_details
  @config = self.class.config
end

Public Instance Methods

height() click to toggle source
# File lib/capybara/paparazzi/shooter.rb, line 161
def height
  @screenshot_size[1]
end
path_and_suffix_for_url(url) click to toggle source
# File lib/capybara/paparazzi/shooter.rb, line 170
def path_and_suffix_for_url(url)
  path = URI.parse(url).path
  path[0] = '' # remove the leading '/'
  path[-1] = '' if path[-1] == '/' # remove the trailing '/'
  path = 'index' if path.empty?
  base = path.gsub('/','-')
  if make_fewer_directories
    unless make_shorter_filenames
      path = path.sub(/[^\/]*$/, '') + base
    end
  else
    path += '/'
    unless make_shorter_filenames
      path += base
    end
  end
  suffix = next_suffix_for_path!(path)
  [ path, suffix ]
end
resize_window(width, height) click to toggle source
# File lib/capybara/paparazzi/shooter.rb, line 165
def resize_window(width, height)
  driver_resize_window(width, height)
  execute_resize_script(height)
end
take_snapshots() click to toggle source
# File lib/capybara/paparazzi/shooter.rb, line 150
def take_snapshots
  set_path_and_suffix
  execute_setup_script
  save_snapshots
  execute_cleanup_script
end
width() click to toggle source
# File lib/capybara/paparazzi/shooter.rb, line 157
def width
  @screenshot_size[0]
end

Private Instance Methods

driver_resize_window(width, height) click to toggle source
# File lib/capybara/paparazzi/shooter.rb, line 199
def driver_resize_window(width, height)
  begin
    if driver.respond_to?(:resize) # Poltergeist
      driver.resize(width, height)
    elsif driver.respond_to?(:resize_window) # Poltergeist
      driver.resize_window(width, height)
    else # Capybara default / Selenium
      driver.resize_window_to(driver.current_window_handle, width, height)
    end
  rescue => e
    log("Error while resizing window: #{e}")
    raise
  end
end
exec_js(script_name, *args) click to toggle source
# File lib/capybara/paparazzi/shooter.rb, line 218
def exec_js(script_name, *args)
  begin
    script = send(script_name)
    if script
      script_text = script.is_a?(Proc) ? script.call(*args) : script.to_s
      driver.execute_script(script_text)
    end
  rescue => e
    log("#{e.message}\nJavascript: #{script_text}")
    raise
  end
end
execute_cleanup_script() click to toggle source
# File lib/capybara/paparazzi/shooter.rb, line 275
def execute_cleanup_script
  exec_js(:js_cleanup_script, self)
end
execute_resize_script(height) click to toggle source
# File lib/capybara/paparazzi/shooter.rb, line 271
def execute_resize_script(height)
  exec_js(:js_resize_script, self, height)
end
execute_setup_script() click to toggle source
# File lib/capybara/paparazzi/shooter.rb, line 214
def execute_setup_script
  exec_js(:js_setup_script, self)
end
get_save_screenshot_args() click to toggle source
# File lib/capybara/paparazzi/shooter.rb, line 283
def get_save_screenshot_args
  [ filename, { full: true } ]
end
log(msg) click to toggle source
# File lib/capybara/paparazzi/shooter.rb, line 291
def log(msg)
  warn "Capybara::Paparazzi: #{msg}"
end
next_suffix_for_path!(path) click to toggle source
# File lib/capybara/paparazzi/shooter.rb, line 287
def next_suffix_for_path!(path)
  self.class.next_suffix_for_path!(path)
end
save_screenshot() click to toggle source
# File lib/capybara/paparazzi/shooter.rb, line 279
def save_screenshot
  driver.save_screenshot(*get_save_screenshot_args)
end
save_snapshot(width_height_rest) click to toggle source
# File lib/capybara/paparazzi/shooter.rb, line 254
def save_snapshot(width_height_rest)
  @do_shot = true
  @screenshot_size = ([] + width_height_rest)

  @filename = "#{file_dir}/#{path}"
  @filename += '-' unless @filename[-1] == '/'
  @filename += "#{width}#{suffix}.png"

  resize_window(width, height)

  before_save_callback.call(self) if before_save_callback

  if @do_shot
    save_screenshot
  end
end
save_snapshots() click to toggle source
# File lib/capybara/paparazzi/shooter.rb, line 231
def save_snapshots
  # In some cases, using screen sizes other than the default
  # can cause failures in Poltergeist, when using the default
  # screen size does not. So be sure to always reset to the
  # original screen size to ensure test behavior consistent with
  # the non-Capybara::Paparazzi tests.
  # Example error:
  # Firing a click at co-ordinates [703, 654] failed. Poltergeist detected another element with CSS selector '' at this position. It may be overlapping the element you are trying to interact with. If you don't care about overlapping elements, try using node.trigger('click').

  begin
    orig_width, orig_height = driver.evaluate_script("[window.innerWidth, window.innerHeight]")
    screenshot_sizes.each do |w_h_rest|
      save_snapshot(w_h_rest)
    end
  ensure
    begin
      driver_resize_window(orig_width, orig_height)
    rescue => e
      log("Error restoring original window size to #{orig_width}x#{orig_height}: #{e}")
    end
  end
end
set_path_and_suffix() click to toggle source
# File lib/capybara/paparazzi/shooter.rb, line 193
def set_path_and_suffix
  path, suffix = path_and_suffix_generator.call(self, driver.current_url)
  self.path = path
  self.suffix = suffix
end