teamcapybara--capybara/lib/capybara/session.rb

242 lines
6.0 KiB
Ruby
Raw Normal View History

2009-11-26 22:47:58 +00:00
module Capybara
class Session
2009-11-04 22:05:11 +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)
@mode = mode
2009-11-26 22:47:58 +00:00
@app = app
end
2009-11-26 22:47:58 +00:00
def driver
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)
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
end
2009-11-26 22:47:58 +00:00
def current_url
driver.current_url
end
def response_headers
driver.response_headers
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
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)
link = wait_for(XPath.link(locator))
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)
button = wait_for(XPath.button(locator))
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
def drag(source_locator, target_locator)
2009-12-14 14:30:51 +00:00
source = wait_for(source_locator)
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)
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={})
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)
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)
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)
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={})
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)
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
driver.body
2009-11-26 22:47:58 +00:00
end
def has_content?(content)
has_xpath?(XPath.content(content).to_s)
2009-11-26 22:47:58 +00:00
end
def has_xpath?(path, options={})
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
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
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
def all(locator)
XPath.wrap(locator).scope(current_scope).paths.map do |path|
driver.find(path)
end.flatten
end
def find(locator)
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
end
2009-12-22 23:14:04 +00:00
def wait_for_condition(script)
begin
2009-12-22 23:14:04 +00:00
Timeout.timeout(Capybara.default_wait_timeout) do
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
end
end
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)
find(XPath.field(locator))
2009-11-26 22:47:58 +00:00
end
alias_method :field_labeled, :find_field
def find_link(locator)
find(XPath.link(locator))
2009-11-26 22:47:58 +00:00
end
def find_button(locator)
find(XPath.button(locator))
2009-11-26 22:47:58 +00:00
end
def evaluate_script(script)
2009-12-14 14:30:29 +00:00
driver.evaluate_script(script)
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