2010-02-23 12:43:40 -05:00
|
|
|
require 'forwardable'
|
2010-07-09 14:30:50 -04:00
|
|
|
require 'capybara/timeout'
|
2009-12-31 14:04:38 -05:00
|
|
|
|
2009-11-26 17:47:58 -05:00
|
|
|
module Capybara
|
|
|
|
class Session
|
2010-02-23 12:43:40 -05:00
|
|
|
extend Forwardable
|
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?,
|
2010-06-29 18:20:23 -04:00
|
|
|
:unselect, :has_select?, :has_no_select?, :current_path, :scope_to
|
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
|
2010-04-07 11:20:59 -04:00
|
|
|
def_delegator :driver, :current_path
|
2010-02-23 12:43:40 -05:00
|
|
|
def_delegator :driver, :response_headers
|
2010-06-06 23:14:40 -04:00
|
|
|
def_delegator :driver, :status_code
|
2010-02-23 12:43:40 -05:00
|
|
|
def_delegator :driver, :visit
|
|
|
|
def_delegator :driver, :body
|
|
|
|
def_delegator :driver, :source
|
2009-11-26 17:47:58 -05:00
|
|
|
|
2010-07-09 19:38:57 -04:00
|
|
|
def document
|
|
|
|
Capybara::Document.new(self)
|
2009-12-13 09:03:19 -05:00
|
|
|
end
|
|
|
|
|
2010-07-09 19:38:57 -04:00
|
|
|
def method_missing(*args)
|
|
|
|
current_node.send(*args)
|
2009-11-24 15:45:52 -05:00
|
|
|
end
|
2009-11-07 12:56:04 -05:00
|
|
|
|
2010-07-09 19:38:57 -04:00
|
|
|
def respond_to?(method)
|
|
|
|
super || current_node.respond_to?(method)
|
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)
|
2010-07-09 19:38:57 -04:00
|
|
|
new_scope = locate(kind, scope, "scope '#{scope || kind}' not found on page")
|
2010-02-11 15:46:31 -05:00
|
|
|
begin
|
2010-07-09 19:38:57 -04:00
|
|
|
scopes.push(new_scope)
|
2010-02-11 15:46:31 -05:00
|
|
|
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
|
|
|
|
2010-07-09 19:42:59 -04:00
|
|
|
def wait_until(timeout = Capybara.default_wait_time)
|
|
|
|
Capybara.timeout(timeout,driver) { yield }
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute_script(script)
|
|
|
|
driver.execute_script(script)
|
|
|
|
end
|
|
|
|
|
|
|
|
def evaluate_script(script)
|
|
|
|
driver.evaluate_script(script)
|
|
|
|
end
|
|
|
|
|
|
|
|
def save_and_open_page
|
|
|
|
require 'capybara/save_and_open_page'
|
|
|
|
Capybara::SaveAndOpenPage.save_and_open_page(body)
|
|
|
|
end
|
|
|
|
|
2009-11-26 17:47:58 -05:00
|
|
|
private
|
|
|
|
|
2010-07-09 19:38:57 -04:00
|
|
|
def current_node
|
|
|
|
scopes.last
|
2009-11-26 17:47:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def scopes
|
2010-07-09 19:38:57 -04:00
|
|
|
@scopes ||= [document]
|
2009-11-26 17:47:58 -05:00
|
|
|
end
|
2009-11-14 09:11:29 -05:00
|
|
|
end
|
2009-11-07 09:36:58 -05:00
|
|
|
end
|