2009-11-26 22:47:58 +00:00
|
|
|
module Capybara
|
|
|
|
class Session
|
2009-11-04 22:05:11 +00:00
|
|
|
|
2009-12-18 18:53:01 +00:00
|
|
|
attr_reader :mode, :app
|
2009-11-09 22:51:39 +00:00
|
|
|
|
2009-11-26 22:47:58 +00:00
|
|
|
def initialize(mode, app)
|
2009-12-18 16:40:51 +00:00
|
|
|
@mode = mode
|
2009-11-26 22:47:58 +00:00
|
|
|
@app = app
|
2009-11-16 19:13:06 +00:00
|
|
|
end
|
2009-12-16 20:55:12 +00:00
|
|
|
|
2009-11-26 22:47:58 +00:00
|
|
|
def driver
|
2009-12-17 23:29:34 +00:00
|
|
|
|
2009-11-26 22:47:58 +00:00
|
|
|
@driver ||= case mode
|
|
|
|
when :rack_test
|
|
|
|
Capybara::Driver::RackTest.new(app)
|
|
|
|
when :selenium
|
|
|
|
Capybara::Driver::Selenium.new(app)
|
2009-12-18 16:40:51 +00:00
|
|
|
when :celerity
|
|
|
|
Capybara::Driver::Celerity.new(app)
|
|
|
|
when :culerity
|
|
|
|
Capybara::Driver::Culerity.new(app)
|
2009-11-26 22:47:58 +00:00
|
|
|
else
|
|
|
|
raise Capybara::DriverNotFoundError, "no driver called #{mode} was found"
|
|
|
|
end
|
2009-11-15 12:40:50 +00:00
|
|
|
end
|
2009-11-26 22:47:58 +00:00
|
|
|
|
2009-12-16 14:16:52 +00:00
|
|
|
def current_url
|
|
|
|
driver.current_url
|
|
|
|
end
|
|
|
|
|
2009-12-17 23:29:34 +00:00
|
|
|
def response_headers
|
2009-12-18 18:33:36 +00:00
|
|
|
driver.response_headers
|
2009-12-17 23:29:34 +00:00
|
|
|
end
|
|
|
|
|
2009-11-26 22:47:58 +00:00
|
|
|
def visit(path)
|
|
|
|
driver.visit(path)
|
2009-11-24 20:32:25 +00:00
|
|
|
end
|
2009-11-26 22:47:58 +00:00
|
|
|
|
2009-12-13 14:03:19 +00:00
|
|
|
def click(locator)
|
|
|
|
link = wait_for(XPath.link(locator).button(locator))
|
|
|
|
raise Capybara::ElementNotFound, "no link or button '#{locator}' found" unless link
|
|
|
|
link.click
|
|
|
|
end
|
|
|
|
|
2009-11-26 22:47:58 +00:00
|
|
|
def click_link(locator)
|
2009-12-12 20:46:08 +00:00
|
|
|
link = wait_for(XPath.link(locator))
|
2009-12-09 15:37:28 +00:00
|
|
|
raise Capybara::ElementNotFound, "no link with title, id or text '#{locator}' found" unless link
|
|
|
|
link.click
|
2009-11-24 20:45:52 +00:00
|
|
|
end
|
2009-11-07 17:56:04 +00:00
|
|
|
|
2009-11-26 22:47:58 +00:00
|
|
|
def click_button(locator)
|
2009-12-12 20:46:08 +00:00
|
|
|
button = wait_for(XPath.button(locator))
|
2009-12-09 15:37:28 +00:00
|
|
|
raise Capybara::ElementNotFound, "no button with value or id or text '#{locator}' found" unless button
|
|
|
|
button.click
|
2009-11-26 22:47:58 +00:00
|
|
|
end
|
2009-11-17 23:03:46 +00:00
|
|
|
|
2009-12-09 09:34:42 +00:00
|
|
|
def drag(source_locator, target_locator)
|
2009-12-14 14:30:51 +00:00
|
|
|
source = wait_for(source_locator)
|
2009-12-09 09:34:42 +00:00
|
|
|
raise Capybara::ElementNotFound, "drag source '#{source_locator}' not found on page" unless source
|
2009-12-14 14:30:51 +00:00
|
|
|
target = wait_for(target_locator)
|
2009-12-09 09:34:42 +00:00
|
|
|
raise Capybara::ElementNotFound, "drag target '#{target_locator}' not found on page" unless target
|
|
|
|
source.drag_to(target)
|
|
|
|
end
|
|
|
|
|
2009-11-26 22:47:58 +00:00
|
|
|
def fill_in(locator, options={})
|
2009-12-12 20:46:08 +00:00
|
|
|
field = wait_for(XPath.fillable_field(locator))
|
2009-12-09 15:52:30 +00:00
|
|
|
raise Capybara::ElementNotFound, "cannot fill in, no text field, text area or password field with id or label '#{locator}' found" unless field
|
|
|
|
field.set(options[:with])
|
2009-11-26 22:47:58 +00:00
|
|
|
end
|
2009-11-17 23:03:46 +00:00
|
|
|
|
2009-11-26 22:47:58 +00:00
|
|
|
def choose(locator)
|
2009-12-12 20:46:08 +00:00
|
|
|
field = wait_for(XPath.radio_button(locator))
|
2009-12-09 15:52:30 +00:00
|
|
|
raise Capybara::ElementNotFound, "cannot choose field, no radio button with id or label '#{locator}' found" unless field
|
|
|
|
field.set(true)
|
2009-11-26 22:47:58 +00:00
|
|
|
end
|
2009-11-07 17:56:04 +00:00
|
|
|
|
2009-11-26 22:47:58 +00:00
|
|
|
def check(locator)
|
2009-12-12 20:46:08 +00:00
|
|
|
field = wait_for(XPath.checkbox(locator))
|
2009-12-09 15:52:30 +00:00
|
|
|
raise Capybara::ElementNotFound, "cannot check field, no checkbox with id or label '#{locator}' found" unless field
|
|
|
|
field.set(true)
|
2009-11-26 22:47:58 +00:00
|
|
|
end
|
2009-11-16 20:41:38 +00:00
|
|
|
|
2009-11-26 22:47:58 +00:00
|
|
|
def uncheck(locator)
|
2009-12-12 20:46:08 +00:00
|
|
|
field = wait_for(XPath.checkbox(locator))
|
2009-12-09 15:52:30 +00:00
|
|
|
raise Capybara::ElementNotFound, "cannot uncheck field, no checkbox with id or label '#{locator}' found" unless field
|
|
|
|
field.set(false)
|
2009-11-26 22:47:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def select(value, options={})
|
2009-12-12 20:46:08 +00:00
|
|
|
field = wait_for(XPath.select(options[:from]))
|
2009-12-09 15:52:30 +00:00
|
|
|
raise Capybara::ElementNotFound, "cannot select option, no select box with id or label '#{options[:from]}' found" unless field
|
|
|
|
field.select(value)
|
2009-11-26 22:47:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def attach_file(locator, path)
|
2009-12-12 20:46:08 +00:00
|
|
|
field = wait_for(XPath.file_field(locator))
|
2009-12-09 15:52:30 +00:00
|
|
|
raise Capybara::ElementNotFound, "cannot attach file, no file field with id or label '#{locator}' found" unless field
|
|
|
|
field.set(path)
|
2009-11-26 22:47:58 +00:00
|
|
|
end
|
2009-11-16 20:41:38 +00:00
|
|
|
|
2009-11-26 22:47:58 +00:00
|
|
|
def body
|
2009-12-18 18:33:36 +00:00
|
|
|
driver.body
|
2009-11-26 22:47:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def has_content?(content)
|
2009-12-09 18:11:52 +00:00
|
|
|
has_xpath?(XPath.content(content).to_s)
|
2009-11-26 22:47:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def has_xpath?(path, options={})
|
2009-12-09 16:00:49 +00:00
|
|
|
results = all(path)
|
2009-11-26 22:47:58 +00:00
|
|
|
if options[:text]
|
|
|
|
results = filter_by_text(results, options[:text])
|
|
|
|
end
|
|
|
|
if options[:count]
|
|
|
|
results.size == options[:count]
|
|
|
|
else
|
|
|
|
results.size > 0
|
2009-11-16 19:13:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-26 22:47:58 +00:00
|
|
|
def has_css?(path, options={})
|
|
|
|
has_xpath?(css_to_xpath(path), options)
|
|
|
|
end
|
2009-11-14 14:11:29 +00:00
|
|
|
|
2009-11-26 22:47:58 +00:00
|
|
|
def within(kind, scope=nil)
|
|
|
|
kind, scope = Capybara.default_selector, kind unless scope
|
|
|
|
scope = css_to_xpath(scope) if kind == :css
|
2009-12-12 20:46:08 +00:00
|
|
|
raise Capybara::ElementNotFound, "scope '#{scope}' not found on page" unless wait_for(scope)
|
2009-11-26 22:47:58 +00:00
|
|
|
scopes.push(scope)
|
|
|
|
yield
|
|
|
|
scopes.pop
|
|
|
|
end
|
2009-11-14 14:11:29 +00:00
|
|
|
|
2009-11-26 22:47:58 +00:00
|
|
|
def within_fieldset(locator)
|
2009-12-09 18:19:11 +00:00
|
|
|
within XPath.fieldset(locator) do
|
2009-11-26 22:47:58 +00:00
|
|
|
yield
|
|
|
|
end
|
2009-11-10 21:48:31 +00:00
|
|
|
end
|
|
|
|
|
2009-11-26 22:47:58 +00:00
|
|
|
def within_table(locator)
|
2009-12-09 18:19:11 +00:00
|
|
|
within XPath.table(locator) do
|
2009-11-26 22:47:58 +00:00
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def save_and_open_page
|
|
|
|
require 'capybara/save_and_open_page'
|
|
|
|
Capybara::SaveAndOpenPage.save_and_open_page(body)
|
|
|
|
end
|
|
|
|
|
2009-12-09 16:00:49 +00:00
|
|
|
def all(locator)
|
2009-12-12 20:46:08 +00:00
|
|
|
XPath.wrap(locator).scope(current_scope).paths.map do |path|
|
|
|
|
driver.find(path)
|
|
|
|
end.flatten
|
2009-12-08 18:29:36 +00:00
|
|
|
end
|
2009-12-12 12:33:00 +00:00
|
|
|
|
2009-12-09 16:00:49 +00:00
|
|
|
def find(locator)
|
2009-12-12 20:46:08 +00:00
|
|
|
all(locator).first
|
|
|
|
end
|
|
|
|
|
|
|
|
def wait_for(locator)
|
|
|
|
return find(locator) unless driver.wait?
|
|
|
|
8.times do
|
|
|
|
result = find(locator)
|
|
|
|
return result if result
|
|
|
|
sleep(0.1)
|
|
|
|
end
|
|
|
|
nil
|
2009-12-09 16:00:49 +00:00
|
|
|
end
|
2009-12-22 23:14:04 +00:00
|
|
|
|
2009-12-20 15:22:47 +00:00
|
|
|
def wait_for_condition(script)
|
|
|
|
begin
|
2009-12-22 23:14:04 +00:00
|
|
|
Timeout.timeout(Capybara.default_wait_timeout) do
|
2009-12-20 15:22:47 +00:00
|
|
|
result = false
|
|
|
|
until result
|
|
|
|
result = evaluate_script(script)
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
rescue Timeout::Error
|
2009-12-22 23:14:04 +00:00
|
|
|
return false
|
2009-12-20 15:22:47 +00:00
|
|
|
end
|
|
|
|
end
|
2009-12-08 18:29:36 +00:00
|
|
|
|
2009-12-22 23:14:04 +00:00
|
|
|
def wait_until(timeout = Capybara.default_wait_timeout, &block)
|
|
|
|
return yield unless driver.wait?
|
|
|
|
|
|
|
|
returned = nil
|
|
|
|
|
|
|
|
Timeout.timeout(timeout) do
|
|
|
|
until returned = yield
|
|
|
|
sleep(0.1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
returned
|
|
|
|
end
|
|
|
|
|
2009-12-09 18:03:55 +00:00
|
|
|
def find_field(locator)
|
2009-12-12 20:46:08 +00:00
|
|
|
find(XPath.field(locator))
|
2009-11-26 22:47:58 +00:00
|
|
|
end
|
|
|
|
alias_method :field_labeled, :find_field
|
|
|
|
|
|
|
|
def find_link(locator)
|
2009-12-12 20:46:08 +00:00
|
|
|
find(XPath.link(locator))
|
2009-11-26 22:47:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_button(locator)
|
2009-12-12 20:46:08 +00:00
|
|
|
find(XPath.button(locator))
|
2009-11-26 22:47:58 +00:00
|
|
|
end
|
|
|
|
|
2009-12-12 12:33:00 +00:00
|
|
|
def evaluate_script(script)
|
2009-12-14 14:30:29 +00:00
|
|
|
driver.evaluate_script(script)
|
2009-12-12 12:33:00 +00:00
|
|
|
end
|
|
|
|
|
2009-11-26 22:47:58 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def css_to_xpath(css)
|
|
|
|
Nokogiri::CSS.xpath_for(css).first
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_by_text(nodes, text)
|
|
|
|
nodes.select do |node|
|
|
|
|
case text
|
|
|
|
when String
|
|
|
|
node.text.include?(text)
|
|
|
|
when Regexp
|
|
|
|
node.text =~ text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_scope
|
|
|
|
scopes.join('')
|
|
|
|
end
|
|
|
|
|
|
|
|
def scopes
|
|
|
|
@scopes ||= []
|
|
|
|
end
|
2009-11-14 14:11:29 +00:00
|
|
|
end
|
2009-11-07 14:36:58 +00:00
|
|
|
end
|