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