class Capybara::Selenium::Driver

Constants

DEFAULT_OPTIONS
SPECIAL_OPTIONS

Attributes

app[R]
options[R]
rack_server[R]

Public Class Methods

new(app, options={}) click to toggle source
# File lib/capybara/selenium/driver.rb, line 28
def initialize(app, options={})
  @app = app
  @options = DEFAULT_OPTIONS.merge(options)
  @rack_server = Capybara::Server.new(@app)
  @rack_server.boot if Capybara.run_server
end

Public Instance Methods

body() click to toggle source
# File lib/capybara/selenium/driver.rb, line 43
def body
  browser.page_source
end
browser() click to toggle source
# File lib/capybara/selenium/driver.rb, line 13
def browser
  unless @browser
    @browser = Selenium::WebDriver.for(options[:browser], options.reject { |key,val| SPECIAL_OPTIONS.include?(key) })

    main = Process.pid
    at_exit do
      # Store the exit status of the test run since it goes away after calling the at_exit proc...
      @exit_status = $!.status if $!.is_a?(SystemExit)
      quit if Process.pid == main
      exit @exit_status if @exit_status # Force exit with stored status
    end
  end
  @browser
end
current_url() click to toggle source
# File lib/capybara/selenium/driver.rb, line 47
def current_url
  browser.current_url
end
evaluate_script(script) click to toggle source
# File lib/capybara/selenium/driver.rb, line 73
def evaluate_script(script)
  browser.execute_script "return #{script}"
end
execute_script(script) click to toggle source
# File lib/capybara/selenium/driver.rb, line 69
def execute_script(script)
  browser.execute_script script
end
find(selector) click to toggle source
# File lib/capybara/selenium/driver.rb, line 51
def find(selector)
  browser.find_elements(:xpath, selector).map { |node| Capybara::Selenium::Node.new(self, node) }
end
find_window( selector ) click to toggle source
# File lib/capybara/selenium/driver.rb, line 98
def find_window( selector )
  original_handle = browser.window_handle
  browser.window_handles.each do |handle|
    browser.switch_to.window handle
    if( selector == browser.execute_script("return window.name") ||
        browser.title.include?(selector) ||
        browser.current_url.include?(selector) ||
        (selector == handle) )
      browser.switch_to.window original_handle
      return handle
    end
  end
  raise Capybara::ElementNotFound, "Could not find a window identified by #{selector}"
end
invalid_element_errors() click to toggle source
# File lib/capybara/selenium/driver.rb, line 124
def invalid_element_errors
  [Selenium::WebDriver::Error::ObsoleteElementError]
end
quit() click to toggle source
# File lib/capybara/selenium/driver.rb, line 118
def quit
  @browser.quit
rescue Errno::ECONNREFUSED
  # Browser must have already gone
end
reset!() click to toggle source
# File lib/capybara/selenium/driver.rb, line 77
def reset!
  # Use instance variable directly so we avoid starting the browser just to reset the session
  if @browser
    begin
      @browser.manage.delete_all_cookies
    rescue Selenium::WebDriver::Error::UnhandledError => e
      # delete_all_cookies fails when we've previously gone
      # to about:blank, so we rescue this error and do nothing
      # instead.
    end
    @browser.navigate.to('about:blank')
  end
end
resynchronize() { || ... } click to toggle source
# File lib/capybara/selenium/driver.rb, line 57
def resynchronize
  if options[:resynchronize]
    load_wait_for_ajax_support
    yield
    Capybara.timeout(options[:resynchronization_timeout], self, "failed to resynchronize, ajax request timed out") do
      evaluate_script("!window.capybaraRequestsOutstanding")
    end
  else
    yield
  end
end
source() click to toggle source
# File lib/capybara/selenium/driver.rb, line 39
def source
  browser.page_source
end
visit(path) click to toggle source
# File lib/capybara/selenium/driver.rb, line 35
def visit(path)
  browser.navigate.to(url(path))
end
wait?() click to toggle source
# File lib/capybara/selenium/driver.rb, line 55
def wait?; true; end
within_frame(frame_id) { || ... } click to toggle source
# File lib/capybara/selenium/driver.rb, line 91
def within_frame(frame_id)
  old_window = browser.window_handle
  browser.switch_to.frame(frame_id)
  yield
  browser.switch_to.window old_window
end
within_window(selector, &blk) click to toggle source
# File lib/capybara/selenium/driver.rb, line 113
def within_window(selector, &blk)
  handle = find_window( selector )
  browser.switch_to.window(handle, &blk)
end