2010-02-23 12:43:40 -05:00
|
|
|
require 'forwardable'
|
2009-12-31 14:04:38 -05:00
|
|
|
require 'capybara/wait_until'
|
|
|
|
|
2009-11-26 17:47:58 -05:00
|
|
|
module Capybara
|
|
|
|
class Session
|
2010-02-23 12:43:40 -05:00
|
|
|
extend Forwardable
|
2009-12-24 00:41:34 -05:00
|
|
|
include Searchable
|
2010-01-01 11:48:39 -05:00
|
|
|
|
2009-12-29 22:05:38 -05:00
|
|
|
DSL_METHODS = [
|
2010-01-11 17:14:52 -05:00
|
|
|
:all, :attach_file, :body, :check, :choose, :click, :click_button, :click_link, :current_url, :drag, :evaluate_script,
|
|
|
|
:field_labeled, :fill_in, :find, :find_button, :find_by_id, :find_field, :find_link, :has_content?, :has_css?,
|
|
|
|
:has_no_content?, :has_no_css?, :has_no_xpath?, :has_xpath?, :locate, :save_and_open_page, :select, :source, :uncheck,
|
2010-04-28 19:03:13 -04:00
|
|
|
:visit, :wait_until, :within, :within_fieldset, :within_table, :within_frame, :has_link?, :has_no_link?, :has_button?,
|
|
|
|
:has_no_button?, :has_field?, :has_no_field?, :has_checked_field?, :has_unchecked_field?, :has_no_table?, :has_table?,
|
|
|
|
:unselect, :has_select?, :has_no_select?
|
2009-12-29 22:05:38 -05:00
|
|
|
]
|
2010-01-11 17:14:52 -05:00
|
|
|
|
2009-12-18 13:53:01 -05:00
|
|
|
attr_reader :mode, :app
|
2009-11-09 17:51:39 -05:00
|
|
|
|
2010-03-12 13:07:15 -05:00
|
|
|
def initialize(mode, app=nil)
|
2009-12-18 11:40:51 -05:00
|
|
|
@mode = mode
|
2009-11-26 17:47:58 -05:00
|
|
|
@app = app
|
2009-11-16 14:13:06 -05:00
|
|
|
end
|
2010-01-01 11:48:39 -05:00
|
|
|
|
2009-11-26 17:47:58 -05:00
|
|
|
def driver
|
2010-02-25 17:51:51 -05:00
|
|
|
@driver ||= begin
|
|
|
|
string = mode.to_s
|
|
|
|
string.gsub!(%r{(^.)|(_.)}) { |m| m[m.length-1,1].upcase }
|
|
|
|
Capybara::Driver.const_get(string.to_sym).new(app)
|
|
|
|
rescue NameError
|
2009-11-26 17:47:58 -05:00
|
|
|
raise Capybara::DriverNotFoundError, "no driver called #{mode} was found"
|
|
|
|
end
|
2009-11-15 07:40:50 -05:00
|
|
|
end
|
2009-11-26 17:47:58 -05:00
|
|
|
|
2010-02-23 12:43:40 -05:00
|
|
|
def_delegator :driver, :cleanup!
|
|
|
|
def_delegator :driver, :current_url
|
|
|
|
def_delegator :driver, :response_headers
|
|
|
|
def_delegator :driver, :visit
|
|
|
|
def_delegator :driver, :body
|
|
|
|
def_delegator :driver, :source
|
2009-11-26 17:47:58 -05:00
|
|
|
|
2009-12-13 09:03:19 -05:00
|
|
|
def click(locator)
|
2009-12-31 14:04:38 -05:00
|
|
|
msg = "no link or button '#{locator}' found"
|
2010-01-17 12:41:38 -05:00
|
|
|
locate(:xpath, XPath.link(locator).button(locator), msg).click
|
2009-12-13 09:03:19 -05:00
|
|
|
end
|
|
|
|
|
2009-11-26 17:47:58 -05:00
|
|
|
def click_link(locator)
|
2009-12-31 14:04:38 -05:00
|
|
|
msg = "no link with title, id or text '#{locator}' found"
|
2010-01-17 12:41:38 -05:00
|
|
|
locate(:xpath, XPath.link(locator), msg).click
|
2009-11-24 15:45:52 -05:00
|
|
|
end
|
2009-11-07 12:56:04 -05:00
|
|
|
|
2009-11-26 17:47:58 -05:00
|
|
|
def click_button(locator)
|
2009-12-31 14:04:38 -05:00
|
|
|
msg = "no button with value or id or text '#{locator}' found"
|
2010-01-29 18:15:03 -05:00
|
|
|
locate(:xpath, XPath.button(locator), msg).click
|
2009-11-26 17:47:58 -05:00
|
|
|
end
|
2009-11-17 18:03:46 -05:00
|
|
|
|
2009-12-09 04:34:42 -05:00
|
|
|
def drag(source_locator, target_locator)
|
2010-01-17 12:41:38 -05:00
|
|
|
source = locate(:xpath, source_locator, "drag source '#{source_locator}' not found on page")
|
|
|
|
target = locate(:xpath, target_locator, "drag target '#{target_locator}' not found on page")
|
2009-12-09 04:34:42 -05:00
|
|
|
source.drag_to(target)
|
|
|
|
end
|
|
|
|
|
2009-11-26 17:47:58 -05:00
|
|
|
def fill_in(locator, options={})
|
2009-12-27 03:11:22 -05:00
|
|
|
msg = "cannot fill in, no text field, text area or password field with id, name, or label '#{locator}' found"
|
2010-04-21 17:55:33 -04:00
|
|
|
raise "Must pass a hash containing 'with'" unless options.kind_of? Hash && !options.index(:with).nil?
|
2010-01-17 12:41:38 -05:00
|
|
|
locate(:xpath, XPath.fillable_field(locator), msg).set(options[:with])
|
2009-11-26 17:47:58 -05:00
|
|
|
end
|
2009-11-17 18:03:46 -05:00
|
|
|
|
2009-11-26 17:47:58 -05:00
|
|
|
def choose(locator)
|
2009-12-27 03:11:22 -05:00
|
|
|
msg = "cannot choose field, no radio button with id, name, or label '#{locator}' found"
|
2010-01-17 12:41:38 -05:00
|
|
|
locate(:xpath, XPath.radio_button(locator), msg).set(true)
|
2009-11-26 17:47:58 -05:00
|
|
|
end
|
2009-11-07 12:56:04 -05:00
|
|
|
|
2009-11-26 17:47:58 -05:00
|
|
|
def check(locator)
|
2009-12-27 03:11:22 -05:00
|
|
|
msg = "cannot check field, no checkbox with id, name, or label '#{locator}' found"
|
2010-01-17 12:41:38 -05:00
|
|
|
locate(:xpath, XPath.checkbox(locator), msg).set(true)
|
2009-11-26 17:47:58 -05:00
|
|
|
end
|
2009-11-16 15:41:38 -05:00
|
|
|
|
2009-11-26 17:47:58 -05:00
|
|
|
def uncheck(locator)
|
2009-12-27 03:11:22 -05:00
|
|
|
msg = "cannot uncheck field, no checkbox with id, name, or label '#{locator}' found"
|
2010-01-17 12:41:38 -05:00
|
|
|
locate(:xpath, XPath.checkbox(locator), msg).set(false)
|
2009-11-26 17:47:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def select(value, options={})
|
2009-12-27 03:11:22 -05:00
|
|
|
msg = "cannot select option, no select box with id, name, or label '#{options[:from]}' found"
|
2010-01-17 12:41:38 -05:00
|
|
|
locate(:xpath, XPath.select(options[:from]), msg).select(value)
|
2009-11-26 17:47:58 -05:00
|
|
|
end
|
|
|
|
|
2010-02-19 15:37:46 -05:00
|
|
|
def unselect(value, options={})
|
|
|
|
msg = "cannot unselect option, no select box with id, name, or label '#{options[:from]}' found"
|
|
|
|
locate(:xpath, XPath.select(options[:from]), msg).unselect(value)
|
|
|
|
end
|
|
|
|
|
2009-11-26 17:47:58 -05:00
|
|
|
def attach_file(locator, path)
|
2009-12-27 03:11:22 -05:00
|
|
|
msg = "cannot attach file, no file field with id, name, or label '#{locator}' found"
|
2010-01-17 12:41:38 -05:00
|
|
|
locate(:xpath, XPath.file_field(locator), msg).set(path)
|
2009-11-26 17:47:58 -05:00
|
|
|
end
|
2009-11-16 15:41:38 -05:00
|
|
|
|
2009-11-26 17:47:58 -05:00
|
|
|
def within(kind, scope=nil)
|
|
|
|
kind, scope = Capybara.default_selector, kind unless scope
|
2009-12-24 00:41:34 -05:00
|
|
|
scope = XPath.from_css(scope) if kind == :css
|
2010-01-17 12:41:38 -05:00
|
|
|
locate(:xpath, scope, "scope '#{scope}' not found on page")
|
2010-02-11 15:46:31 -05:00
|
|
|
begin
|
|
|
|
scopes.push(scope)
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
scopes.pop
|
|
|
|
end
|
2009-11-26 17:47:58 -05:00
|
|
|
end
|
2009-11-14 09:11:29 -05:00
|
|
|
|
2009-11-26 17:47:58 -05:00
|
|
|
def within_fieldset(locator)
|
2010-01-21 07:40:08 -05:00
|
|
|
within :xpath, XPath.fieldset(locator) do
|
2009-11-26 17:47:58 -05:00
|
|
|
yield
|
|
|
|
end
|
2009-11-10 16:48:31 -05:00
|
|
|
end
|
|
|
|
|
2009-11-26 17:47:58 -05:00
|
|
|
def within_table(locator)
|
2010-01-21 07:40:08 -05:00
|
|
|
within :xpath, XPath.table(locator) do
|
2009-11-26 17:47:58 -05:00
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
2010-01-01 12:29:30 -05:00
|
|
|
|
2010-05-09 10:51:11 -04:00
|
|
|
def within_frame(frame_id)
|
2010-04-28 19:03:13 -04:00
|
|
|
driver.within_frame(frame_id) do
|
2009-11-26 17:47:58 -05:00
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
2010-01-01 12:29:30 -05:00
|
|
|
|
|
|
|
def has_xpath?(path, options={})
|
2010-01-01 13:36:45 -05:00
|
|
|
wait_conditionally_until do
|
2010-01-28 16:47:35 -05:00
|
|
|
results = all(:xpath, path, options)
|
2010-01-01 13:36:45 -05:00
|
|
|
|
|
|
|
if options[:count]
|
|
|
|
results.size == options[:count]
|
|
|
|
else
|
|
|
|
results.size > 0
|
|
|
|
end
|
2010-01-01 12:29:30 -05:00
|
|
|
end
|
2010-01-01 13:36:45 -05:00
|
|
|
rescue Capybara::TimeoutError
|
|
|
|
return false
|
2010-01-01 12:29:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def has_no_xpath?(path, options={})
|
2010-01-01 13:36:45 -05:00
|
|
|
wait_conditionally_until do
|
2010-01-28 16:47:35 -05:00
|
|
|
results = all(:xpath, path, options)
|
2010-01-01 13:36:45 -05:00
|
|
|
|
|
|
|
if options[:count]
|
|
|
|
results.size != options[:count]
|
|
|
|
else
|
|
|
|
results.empty?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue Capybara::TimeoutError
|
|
|
|
return false
|
2010-01-01 12:29:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def has_css?(path, options={})
|
|
|
|
has_xpath?(XPath.from_css(path), options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_no_css?(path, options={})
|
2010-01-01 13:36:45 -05:00
|
|
|
has_no_xpath?(XPath.from_css(path), options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_content?(content)
|
|
|
|
has_xpath?(XPath.content(content))
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_no_content?(content)
|
|
|
|
has_no_xpath?(XPath.content(content))
|
2010-01-01 12:29:30 -05:00
|
|
|
end
|
2009-11-26 17:47:58 -05:00
|
|
|
|
2010-01-18 15:28:06 -05:00
|
|
|
def has_link?(locator)
|
|
|
|
has_xpath?(XPath.link(locator))
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_no_link?(locator)
|
|
|
|
has_no_xpath?(XPath.link(locator))
|
|
|
|
end
|
|
|
|
|
2010-01-18 15:40:59 -05:00
|
|
|
def has_button?(locator)
|
|
|
|
has_xpath?(XPath.button(locator))
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_no_button?(locator)
|
|
|
|
has_no_xpath?(XPath.button(locator))
|
|
|
|
end
|
|
|
|
|
2010-01-18 15:28:06 -05:00
|
|
|
def has_field?(locator, options={})
|
|
|
|
has_xpath?(XPath.field(locator, options))
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_no_field?(locator, options={})
|
|
|
|
has_no_xpath?(XPath.field(locator, options))
|
|
|
|
end
|
|
|
|
|
2010-01-18 16:31:22 -05:00
|
|
|
def has_checked_field?(locator)
|
|
|
|
has_xpath?(XPath.field(locator, :checked => true))
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_unchecked_field?(locator)
|
|
|
|
has_xpath?(XPath.field(locator, :unchecked => true))
|
|
|
|
end
|
|
|
|
|
2010-02-20 14:43:32 -05:00
|
|
|
def has_select?(locator, options={})
|
|
|
|
has_xpath?(XPath.select(locator, options))
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_no_select?(locator, options={})
|
|
|
|
has_no_xpath?(XPath.select(locator, options))
|
|
|
|
end
|
|
|
|
|
2010-02-20 16:41:53 -05:00
|
|
|
def has_table?(locator, options={})
|
|
|
|
has_xpath?(XPath.table(locator, options))
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_no_table?(locator, options={})
|
|
|
|
has_no_xpath?(XPath.table(locator, options))
|
|
|
|
end
|
|
|
|
|
2009-11-26 17:47:58 -05:00
|
|
|
def save_and_open_page
|
|
|
|
require 'capybara/save_and_open_page'
|
|
|
|
Capybara::SaveAndOpenPage.save_and_open_page(body)
|
|
|
|
end
|
|
|
|
|
2009-12-31 14:04:38 -05:00
|
|
|
#return node identified by locator or raise ElementNotFound(using desc)
|
2010-01-17 12:41:38 -05:00
|
|
|
def locate(kind_or_locator, locator=nil, fail_msg = nil)
|
|
|
|
node = wait_conditionally_until { find(kind_or_locator, locator) }
|
2010-01-01 11:48:03 -05:00
|
|
|
ensure
|
2010-01-17 12:41:38 -05:00
|
|
|
raise Capybara::ElementNotFound, fail_msg || "Unable to locate '#{kind_or_locator}'" unless node
|
2010-01-01 11:48:03 -05:00
|
|
|
return node
|
2009-12-09 11:00:49 -05:00
|
|
|
end
|
2010-01-01 11:48:39 -05:00
|
|
|
|
2009-12-31 14:04:38 -05:00
|
|
|
def wait_until(timeout = Capybara.default_wait_time)
|
2010-02-25 17:51:51 -05:00
|
|
|
WaitUntil.timeout(timeout,driver) { yield }
|
2009-12-20 10:22:47 -05:00
|
|
|
end
|
2010-01-01 11:48:39 -05:00
|
|
|
|
2009-12-12 07:33:00 -05:00
|
|
|
def evaluate_script(script)
|
2009-12-14 09:30:29 -05:00
|
|
|
driver.evaluate_script(script)
|
2009-12-12 07:33:00 -05:00
|
|
|
end
|
|
|
|
|
2009-11-26 17:47:58 -05:00
|
|
|
private
|
|
|
|
|
2010-01-01 13:36:45 -05:00
|
|
|
def wait_conditionally_until
|
|
|
|
if driver.wait? then wait_until { yield } else yield end
|
|
|
|
end
|
|
|
|
|
2009-12-24 00:41:34 -05:00
|
|
|
def all_unfiltered(locator)
|
|
|
|
XPath.wrap(locator).scope(current_scope).paths.map do |path|
|
|
|
|
driver.find(path)
|
|
|
|
end.flatten
|
2009-11-26 17:47:58 -05:00
|
|
|
end
|
2010-01-01 11:48:39 -05:00
|
|
|
|
2009-11-26 17:47:58 -05:00
|
|
|
def current_scope
|
|
|
|
scopes.join('')
|
|
|
|
end
|
|
|
|
|
|
|
|
def scopes
|
|
|
|
@scopes ||= []
|
|
|
|
end
|
2009-11-14 09:11:29 -05:00
|
|
|
end
|
2009-11-07 09:36:58 -05:00
|
|
|
end
|