Class: Capybara::Selenium::Driver

Inherits:
Driver::Base show all
Defined in:
lib/capybara/selenium/driver.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :browser => :firefox
}
SPECIAL_OPTIONS =
[:browser]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Driver::Base

#response_headers, #status_code

Constructor Details

#initialize(app, options = {}) ⇒ Driver

Returns a new instance of Driver.



26
27
28
29
30
31
# File 'lib/capybara/selenium/driver.rb', line 26

def initialize(app, options={})
  @app = app
  @browser = nil
  @exit_status = nil
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



9
10
11
# File 'lib/capybara/selenium/driver.rb', line 9

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/capybara/selenium/driver.rb', line 9

def options
  @options
end

Instance Method Details

#browserObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/capybara/selenium/driver.rb', line 11

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_urlObject



41
42
43
# File 'lib/capybara/selenium/driver.rb', line 41

def current_url
  browser.current_url
end

#evaluate_script(script) ⇒ Object



56
57
58
# File 'lib/capybara/selenium/driver.rb', line 56

def evaluate_script(script)
  browser.execute_script "return #{script}"
end

#execute_script(script) ⇒ Object



52
53
54
# File 'lib/capybara/selenium/driver.rb', line 52

def execute_script(script)
  browser.execute_script script
end

#find(selector) ⇒ Object



45
46
47
# File 'lib/capybara/selenium/driver.rb', line 45

def find(selector)
  browser.find_elements(:xpath, selector).map { |node| Capybara::Selenium::Node.new(self, node) }
end

#find_window(selector) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/capybara/selenium/driver.rb', line 85

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

#htmlObject



37
38
39
# File 'lib/capybara/selenium/driver.rb', line 37

def html
  browser.page_source
end

#invalid_element_errorsObject



111
112
113
# File 'lib/capybara/selenium/driver.rb', line 111

def invalid_element_errors
  [Selenium::WebDriver::Error::StaleElementReferenceError, Selenium::WebDriver::Error::UnhandledError, Selenium::WebDriver::Error::ElementNotVisibleError]
end

#needs_server?Boolean

Returns:

  • (Boolean)


50
# File 'lib/capybara/selenium/driver.rb', line 50

def needs_server?; true; end

#quitObject



105
106
107
108
109
# File 'lib/capybara/selenium/driver.rb', line 105

def quit
  @browser.quit
rescue Errno::ECONNREFUSED
  # Browser must have already gone
end

#reset!Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/capybara/selenium/driver.rb', line 64

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
      # 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

#save_screenshot(path, options = {}) ⇒ Object



60
61
62
# File 'lib/capybara/selenium/driver.rb', line 60

def save_screenshot(path, options={})
  browser.save_screenshot(path)
end

#visit(path) ⇒ Object



33
34
35
# File 'lib/capybara/selenium/driver.rb', line 33

def visit(path)
  browser.navigate.to(path)
end

#wait?Boolean

Returns:

  • (Boolean)


49
# File 'lib/capybara/selenium/driver.rb', line 49

def wait?; true; end

#within_frame(frame_id) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/capybara/selenium/driver.rb', line 77

def within_frame(frame_id)
  old_window = browser.window_handle
  browser.switch_to.frame(frame_id)
  yield
ensure
  browser.switch_to.window old_window
end

#within_window(selector, &blk) ⇒ Object



100
101
102
103
# File 'lib/capybara/selenium/driver.rb', line 100

def within_window(selector, &blk)
  handle = find_window( selector )
  browser.switch_to.window(handle, &blk)
end