2009-11-14 09:44:46 +00:00
|
|
|
require 'selenium-webdriver'
|
2009-11-07 15:30:16 +00:00
|
|
|
|
2009-12-09 20:43:40 +00:00
|
|
|
class Capybara::Driver::Selenium < Capybara::Driver::Base
|
2010-07-09 18:08:33 +00:00
|
|
|
class Node < Capybara::Driver::Node
|
2009-11-07 15:30:16 +00:00
|
|
|
def text
|
2010-07-15 18:55:12 +00:00
|
|
|
native.text
|
2009-11-07 15:30:16 +00:00
|
|
|
end
|
2009-11-14 09:44:46 +00:00
|
|
|
|
|
|
|
def [](name)
|
|
|
|
if name == :value
|
2010-07-15 18:55:12 +00:00
|
|
|
native.value
|
2009-11-07 15:30:16 +00:00
|
|
|
else
|
2010-07-15 18:55:12 +00:00
|
|
|
native.attribute(name.to_s)
|
2009-11-07 15:30:16 +00:00
|
|
|
end
|
2009-11-14 09:44:46 +00:00
|
|
|
rescue Selenium::WebDriver::Error::WebDriverError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2010-02-25 18:37:22 +00:00
|
|
|
def value
|
|
|
|
if tag_name == "select" and self[:multiple]
|
2010-07-15 18:55:12 +00:00
|
|
|
native.find_elements(:xpath, ".//option").select { |n| n.selected? }.map { |n| n.text }
|
2010-02-25 18:37:22 +00:00
|
|
|
else
|
2010-07-10 01:11:54 +00:00
|
|
|
self[:value]
|
2010-02-25 18:37:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-14 09:44:46 +00:00
|
|
|
def set(value)
|
2010-06-16 20:06:20 +00:00
|
|
|
if tag_name == 'input' and type == 'radio'
|
2010-07-15 18:55:12 +00:00
|
|
|
native.click
|
2009-11-14 09:44:46 +00:00
|
|
|
elsif tag_name == 'input' and type == 'checkbox'
|
2010-08-21 14:15:19 +00:00
|
|
|
native.click if value ^ native.attribute('checked').to_s.eql?("true")
|
2010-06-16 20:06:20 +00:00
|
|
|
elsif tag_name == 'textarea' or tag_name == 'input'
|
2010-07-15 18:55:12 +00:00
|
|
|
native.clear
|
|
|
|
native.send_keys(value.to_s)
|
2009-11-14 09:44:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-14 11:42:53 +00:00
|
|
|
def select_option
|
|
|
|
native.select
|
2009-11-07 15:30:16 +00:00
|
|
|
end
|
2009-11-14 09:44:46 +00:00
|
|
|
|
2010-08-14 11:42:53 +00:00
|
|
|
def unselect_option
|
2010-08-21 14:15:19 +00:00
|
|
|
if select_node['multiple'] != 'multiple' and select_node['multiple'] != 'true'
|
2010-08-14 11:42:53 +00:00
|
|
|
raise Capybara::UnselectNotAllowed, "Cannot unselect option from single select box."
|
2010-02-19 21:58:50 +00:00
|
|
|
end
|
2010-08-14 11:42:53 +00:00
|
|
|
native.clear
|
2010-02-19 21:58:50 +00:00
|
|
|
end
|
|
|
|
|
2009-11-07 15:30:16 +00:00
|
|
|
def click
|
2010-07-15 18:55:12 +00:00
|
|
|
native.click
|
2009-11-07 15:30:16 +00:00
|
|
|
end
|
2009-11-14 09:44:46 +00:00
|
|
|
|
2009-12-05 18:40:15 +00:00
|
|
|
def drag_to(element)
|
2010-07-15 18:55:12 +00:00
|
|
|
native.drag_and_drop_on(element.native)
|
2009-12-05 18:40:15 +00:00
|
|
|
end
|
|
|
|
|
2009-11-07 15:30:16 +00:00
|
|
|
def tag_name
|
2010-07-15 18:55:12 +00:00
|
|
|
native.tag_name
|
2009-11-14 09:44:46 +00:00
|
|
|
end
|
2010-01-01 21:46:05 +00:00
|
|
|
|
2009-12-22 20:13:55 +00:00
|
|
|
def visible?
|
2010-07-15 18:55:12 +00:00
|
|
|
native.displayed? and native.displayed? != "false"
|
2009-12-22 20:13:55 +00:00
|
|
|
end
|
2010-09-15 20:38:38 +00:00
|
|
|
|
2010-07-09 23:58:34 +00:00
|
|
|
def find(locator)
|
2010-07-15 18:55:12 +00:00
|
|
|
native.find_elements(:xpath, locator).map { |n| self.class.new(driver, n) }
|
2010-02-26 17:38:22 +00:00
|
|
|
end
|
|
|
|
|
2010-07-09 23:58:34 +00:00
|
|
|
private
|
|
|
|
|
2010-08-14 11:42:53 +00:00
|
|
|
# a reference to the select node if this is an option node
|
|
|
|
def select_node
|
|
|
|
find('./ancestor::select').first
|
|
|
|
end
|
|
|
|
|
2009-11-14 09:44:46 +00:00
|
|
|
def type
|
|
|
|
self[:type]
|
2009-11-07 15:30:16 +00:00
|
|
|
end
|
2009-11-14 09:44:46 +00:00
|
|
|
|
2009-11-07 15:30:16 +00:00
|
|
|
end
|
2009-11-14 09:44:46 +00:00
|
|
|
|
2010-09-07 16:35:55 +00:00
|
|
|
attr_reader :app, :rack_server, :options
|
2009-11-07 15:30:16 +00:00
|
|
|
|
2010-08-30 07:19:44 +00:00
|
|
|
def browser
|
|
|
|
unless @browser
|
2010-09-15 20:38:38 +00:00
|
|
|
@browser = Selenium::WebDriver.for(options.delete(:browser) || :firefox, options)
|
2009-11-14 09:44:46 +00:00
|
|
|
at_exit do
|
2010-08-30 07:19:44 +00:00
|
|
|
@browser.quit
|
2009-11-14 09:44:46 +00:00
|
|
|
end
|
|
|
|
end
|
2010-08-30 07:19:44 +00:00
|
|
|
@browser
|
2009-11-07 15:30:16 +00:00
|
|
|
end
|
|
|
|
|
2010-09-07 16:35:55 +00:00
|
|
|
def initialize(app, options={})
|
2009-11-07 15:30:16 +00:00
|
|
|
@app = app
|
2010-09-07 16:35:55 +00:00
|
|
|
@options = options
|
2010-01-17 16:40:26 +00:00
|
|
|
@rack_server = Capybara::Server.new(@app)
|
|
|
|
@rack_server.boot if Capybara.run_server
|
2009-11-07 15:30:16 +00:00
|
|
|
end
|
2009-11-14 09:44:46 +00:00
|
|
|
|
2009-11-07 15:30:16 +00:00
|
|
|
def visit(path)
|
2010-01-01 19:13:54 +00:00
|
|
|
browser.navigate.to(url(path))
|
2009-11-07 15:30:16 +00:00
|
|
|
end
|
2009-11-14 09:44:46 +00:00
|
|
|
|
2010-01-01 22:58:59 +00:00
|
|
|
def source
|
|
|
|
browser.page_source
|
|
|
|
end
|
|
|
|
|
2009-12-18 17:50:55 +00:00
|
|
|
def body
|
2010-01-01 19:13:54 +00:00
|
|
|
browser.page_source
|
2009-11-07 15:30:16 +00:00
|
|
|
end
|
2009-11-14 09:44:46 +00:00
|
|
|
|
2009-12-16 14:16:52 +00:00
|
|
|
def current_url
|
2010-01-01 19:13:54 +00:00
|
|
|
browser.current_url
|
2009-12-16 14:16:52 +00:00
|
|
|
end
|
|
|
|
|
2009-11-07 15:30:16 +00:00
|
|
|
def find(selector)
|
2010-01-01 19:13:54 +00:00
|
|
|
browser.find_elements(:xpath, selector).map { |node| Node.new(self, node) }
|
2009-11-07 15:30:16 +00:00
|
|
|
end
|
2009-12-12 20:46:08 +00:00
|
|
|
|
|
|
|
def wait?; true; end
|
2009-11-07 15:30:16 +00:00
|
|
|
|
2010-06-29 20:41:33 +00:00
|
|
|
def execute_script(script)
|
|
|
|
browser.execute_script script
|
|
|
|
end
|
|
|
|
|
2009-12-11 21:41:12 +00:00
|
|
|
def evaluate_script(script)
|
2010-01-01 19:13:54 +00:00
|
|
|
browser.execute_script "return #{script}"
|
|
|
|
end
|
2010-01-01 21:46:05 +00:00
|
|
|
|
2010-07-29 13:25:45 +00:00
|
|
|
def reset!
|
2010-09-07 16:35:09 +00:00
|
|
|
# Use instance variable directly so we avoid starting the browser just to reset the session
|
|
|
|
@browser.manage.delete_all_cookies if @browser
|
2010-01-23 11:58:30 +00:00
|
|
|
end
|
|
|
|
|
2010-01-12 20:27:39 +00:00
|
|
|
def within_frame(frame_id)
|
2010-04-28 23:03:13 +00:00
|
|
|
old_window = browser.window_handle
|
|
|
|
browser.switch_to.frame(frame_id)
|
|
|
|
yield
|
|
|
|
browser.switch_to.window old_window
|
2010-01-12 19:40:10 +00:00
|
|
|
end
|
2010-04-28 23:03:13 +00:00
|
|
|
|
2010-08-28 16:50:34 +00:00
|
|
|
def within_window(handle, &blk)
|
|
|
|
browser.switch_to.window(handle, &blk)
|
2010-08-27 19:00:08 +00:00
|
|
|
end
|
|
|
|
|
2009-11-07 15:30:16 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def url(path)
|
2010-01-17 16:40:26 +00:00
|
|
|
rack_server.url(path)
|
2009-11-07 15:30:16 +00:00
|
|
|
end
|
2009-11-14 09:44:46 +00:00
|
|
|
|
2009-11-07 15:30:16 +00:00
|
|
|
end
|