Class: Sauce::Capybara::Driver

Inherits:
Capybara::Selenium::Driver
  • Object
show all
Defined in:
lib/sauce/capybara.rb

Constant Summary collapse

RETRY_ON =
[::Selenium::WebDriver::Error::UnhandledError,
::Selenium::WebDriver::Error::UnknownError]
MAX_RETRIES =
3

Instance Method Summary collapse

Instance Method Details

#browserObject

Oh gods why didn’t I comment these when I wrote them? These are what I think I’m doing.

Returns the browser currently being used or fetches a new browser, either from the RSpec integration or by creating one.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sauce/capybara.rb', line 82

def browser
  # Return the existing browser if we have it
  unless existing_browser?
    # Try to get a driver from the driver pool
    @browser = rspec_browser
    unless @browser
      @browser = Sauce::Selenium2.new
      at_exit do
        finish!
      end
    end
  end
  @browser
end

#existing_browser?Boolean

If a browser has been created OR RSpec has put one in the diriver pool and we’re using that browser, returns true.

Returns:

  • (Boolean)


109
110
111
112
113
114
115
# File 'lib/sauce/capybara.rb', line 109

def existing_browser?
  if @using_rspec_browser
    @browser == Sauce.driver_pool[Thread.current.object_id]
  else
    @browser
  end
end

#finish!Object



117
118
119
120
121
122
123
124
# File 'lib/sauce/capybara.rb', line 117

def finish!
  @browser.quit if existing_browser?
  @browser = nil

  # Rethink how to do this.  RSpec still references the driver pool.
  Sauce.driver_pool[Thread.current.object_id] = nil
  @using_rspec_browser = nil
end

#handle_retry(method, *args, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sauce/capybara.rb', line 16

def handle_retry(method, *args, &block)
  retries = 0

  # Disable retries only when we really really want to, this will remain
  # an undocomented hack for the time being
  if ENV['SAUCE_DISABLE_RETRY']
    retries = MAX_RETRIES
  end

  begin
    send("base_#{method}".to_sym, *args, &block)
  rescue *RETRY_ON => e
    if retries < MAX_RETRIES
      puts "Received an exception (#{e}), retrying"
      retries = retries + 1
      retry
    else
      raise
    end
  end
end

#render(path) ⇒ Object



126
127
128
# File 'lib/sauce/capybara.rb', line 126

def render(path)
  browser.save_screenshot path
end

#rspec_browserObject

Returns the rspec created browser if it exists



98
99
100
101
102
103
104
105
# File 'lib/sauce/capybara.rb', line 98

def rspec_browser
  if browser = Sauce.driver_pool[Thread.current.object_id]
    @using_rspec_browser = true
  else
    @using_rspec_browser = false
  end
  browser
end