1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00
teamcapybara--capybara/lib/capybara/session.rb

111 lines
2.8 KiB
Ruby
Raw Normal View History

require 'forwardable'
2010-07-09 14:30:50 -04:00
require 'capybara/timeout'
2009-11-26 17:47:58 -05:00
module Capybara
class Session
extend Forwardable
2010-01-01 11:48:39 -05:00
DSL_METHODS = [
: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,
: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
]
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)
@mode = mode
2009-11-26 17:47:58 -05:00
@app = app
end
2010-01-01 11:48:39 -05:00
2009-11-26 17:47:58 -05:00
def driver
@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
end
2009-11-26 17:47:58 -05:00
def_delegator :driver, :cleanup!
def_delegator :driver, :current_url
2010-04-07 11:20:59 -04:00
def_delegator :driver, :current_path
def_delegator :driver, :response_headers
def_delegator :driver, :status_code
def_delegator :driver, :visit
def_delegator :driver, :body
def_delegator :driver, :source
2009-11-26 17:47:58 -05:00
def document
Capybara::Document.new(self)
end
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
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)
new_scope = locate(kind, scope, "scope '#{scope || kind}' not found on page")
begin
scopes.push(new_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)
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)
within :xpath, XPath.table(locator) do
2009-11-26 17:47:58 -05:00
yield
end
end
2010-05-09 10:51:11 -04:00
def within_frame(frame_id)
driver.within_frame(frame_id) do
2009-11-26 17:47:58 -05:00
yield
end
end
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
def current_node
scopes.last
2009-11-26 17:47:58 -05:00
end
def scopes
@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