Class: Compatriot::Browser

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL
Defined in:
lib/compatriot/browser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Browser



19
20
21
22
23
24
25
# File 'lib/compatriot/browser.rb', line 19

def initialize(params = {})
  @name                 = params[:name]
  @screenshot_directory = params[:screenshot_directory]
  @screenshot_locations = {}

  create_screenshot_path
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/compatriot/browser.rb', line 17

def name
  @name
end

Class Method Details

.create_browsers(params = {}) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/compatriot/browser.rb', line 8

def self.create_browsers(params = {})
  params[:browser_names].collect do |b|
    Compatriot::Browser.new(
      :name => b,
      :screenshot_directory => params[:results_directory]
    )
  end
end

Instance Method Details

#absolute_screenshot_for(path) ⇒ Object



48
49
50
51
52
# File 'lib/compatriot/browser.rb', line 48

def absolute_screenshot_for(path)
  if @screenshot_locations[path]
    absolute_dir(@screenshot_locations[path])
  end
end

#initialize_capybara(app) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/compatriot/browser.rb', line 31

def initialize_capybara(app)
  driver = "selenium_#{@name}".to_sym
  Capybara.register_driver driver do |a|
    Capybara::Selenium::Driver.new(a, :browser => @name.to_sym)
  end
  Capybara.default_driver = driver
  Capybara.app = app
end

#relative_screenshot_for(path) ⇒ Object



54
55
56
57
58
# File 'lib/compatriot/browser.rb', line 54

def relative_screenshot_for(path)
  if @screenshot_locations[path]
    File.join(name, @screenshot_locations[path])
  end
end

#screenshot_pathObject



27
28
29
# File 'lib/compatriot/browser.rb', line 27

def screenshot_path
  File.join(@screenshot_directory, @name)
end

#take_screenshot(path) ⇒ Object



60
61
62
63
64
65
# File 'lib/compatriot/browser.rb', line 60

def take_screenshot(path)
  visit path
  filename = next_filename
  Capybara.page.driver.browser.save_screenshot(absolute_dir(filename))
  @screenshot_locations[path] = filename
end

#take_screenshots(params = {}) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/compatriot/browser.rb', line 40

def take_screenshots(params = {})
  initialize_capybara(params[:app])
  params[:paths].map { |path| take_screenshot(path) }

  # Reset the selenium session to avoid timeout errors
  Capybara.send(:session_pool).delete_if { |key, value| key =~ /selenium/i }
end