Module: Waterpig::BrowserTools

Defined in:
lib/waterpig/browser-tools.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.warn(general, specific = nil) ⇒ Object



28
29
30
31
32
33
# File 'lib/waterpig/browser-tools.rb', line 28

def self.warn(general, specific=nil)
  warnings.fetch(general) do
    warnings[general] = true
    puts "Warning: #{general}#{specific ? ": #{specific}" : ""}"
  end
end

.warningsObject



24
25
26
# File 'lib/waterpig/browser-tools.rb', line 24

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

Instance Method Details

#accept_alertObject



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/waterpig/browser-tools.rb', line 3

def accept_alert
  if poltergeist?
    # do nothing ... really?
    # https://github.com/jonleighton/poltergeist/issues/50
    # Poltergeist's behavior is to return true to window.alert
    # Does mean it's a challenge to test cancelling e.g. deletes, or
    # confirming that an alert happens even
  else
    alert = page.driver.browser.switch_to.alert
    alert.accept
  end
end

#attr_includes(attr, value) ⇒ Object

renders the xpath to properly match a css class (or other space separated attribute) Use like: div[#“findme”)]



39
40
41
# File 'lib/waterpig/browser-tools.rb', line 39

def attr_includes(attr, value)
  "contains(concat(' ', normalize-space(@#{attr}), ' '), ' #{value} ')"
end

#class_includes(value) ⇒ Object



43
44
45
# File 'lib/waterpig/browser-tools.rb', line 43

def class_includes(value)
  attr_includes("class", value)
end

#frame_index(dir) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/waterpig/browser-tools.rb', line 47

def frame_index(dir)
  @frame_dirs ||= Hash.new do |h,k|
    FileUtils.rm_rf(k)
    FileUtils.mkdir_p(k)
    h[k] = 0
  end
  @frame_dirs[dir] += 1
end

#poltergeist?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/waterpig/browser-tools.rb', line 20

def poltergeist?
  Capybara.javascript_driver.to_s =~ /poltergeist/
end

#save_snapshot(dir, name) {|img_path| ... } ⇒ Object

Yields:

  • (img_path)


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
# File 'lib/waterpig/browser-tools.rb', line 56

def save_snapshot(dir, name)
  require 'fileutils'

  dir = File.join(RSpec.configuration.waterpig_snapshot_dir, dir)
  base_path = "#{dir}/#{"%03i" % frame_index(dir)}-#{name}"

  img_path = base_path + ".png"
  begin
    page.driver.save_screenshot(img_path, :full => true)
  rescue Capybara::NotSupportedByDriverError => nsbde
    BrowserTools.warn("Can't use snapshot", nsbde.inspect)
  rescue Object => ex
    BrowserTools.warn("Error when attempting snapshot", ex.inspect)
  end

  html_path = base_path + ".html"
  begin
    File::open(html_path, "w") do |file|
      file.write page.html
    end
  rescue Capybara::NotSupportedByDriverError => nsbde
    BrowserTools.warn("Can't use page html", nsbde.inspect)
  rescue Object => ex
    BrowserTools.warn("Error in attempted html save", ex.inspect)
  end

  yield img_path if block_given?
end

#snapshot(dir) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/waterpig/browser-tools.rb', line 85

def snapshot(dir)
  save_snapshot(dir, "debug") do |path|
    msg = "Saved screenshot: #{path} (from: #{caller[0].sub(/^#{Dir.pwd}/,'')})"
    puts msg
    Rails.logger.info(msg)
  end
end

#wait_for_animationObject



16
17
18
# File 'lib/waterpig/browser-tools.rb', line 16

def wait_for_animation
  sleep(1) if poltergeist?
end