2009-11-16 21:02:16 +00:00
|
|
|
module Capybara
|
2009-11-12 17:09:29 +00:00
|
|
|
class << self
|
2009-11-16 19:11:42 +00:00
|
|
|
attr_writer :default_driver, :current_driver, :javascript_driver
|
2009-11-12 16:51:31 +00:00
|
|
|
|
2009-11-12 17:57:46 +00:00
|
|
|
attr_accessor :app
|
|
|
|
|
2009-11-12 17:09:29 +00:00
|
|
|
def default_driver
|
|
|
|
@default_driver || :rack_test
|
|
|
|
end
|
2009-11-12 16:51:31 +00:00
|
|
|
|
2009-11-12 17:09:29 +00:00
|
|
|
def current_driver
|
2009-11-26 16:43:39 +00:00
|
|
|
@current_driver || default_driver
|
2009-11-12 17:09:29 +00:00
|
|
|
end
|
|
|
|
alias_method :mode, :current_driver
|
2009-12-12 12:33:00 +00:00
|
|
|
|
2009-11-16 19:11:42 +00:00
|
|
|
def javascript_driver
|
|
|
|
@javascript_driver || :selenium
|
|
|
|
end
|
2009-11-12 16:51:31 +00:00
|
|
|
|
2009-11-12 17:09:29 +00:00
|
|
|
def use_default_driver
|
2009-11-26 16:43:39 +00:00
|
|
|
@current_driver = nil
|
2009-11-12 17:09:29 +00:00
|
|
|
end
|
2009-11-12 16:51:31 +00:00
|
|
|
|
2009-11-12 17:57:46 +00:00
|
|
|
def current_session
|
2009-12-08 18:45:54 +00:00
|
|
|
session_pool["#{current_driver}#{app.object_id}"] ||= Capybara::Session.new(current_driver, app)
|
2009-11-26 16:43:39 +00:00
|
|
|
end
|
2009-12-12 12:33:00 +00:00
|
|
|
|
2009-11-26 16:43:39 +00:00
|
|
|
def current_session?
|
|
|
|
session_pool.has_key?("#{current_driver}#{app.object_id}")
|
2009-11-12 17:57:46 +00:00
|
|
|
end
|
2009-12-12 12:33:00 +00:00
|
|
|
|
2009-11-15 22:20:37 +00:00
|
|
|
def reset_sessions!
|
|
|
|
@session_pool = nil
|
|
|
|
end
|
2009-11-25 17:57:32 +00:00
|
|
|
|
2009-11-12 17:57:46 +00:00
|
|
|
private
|
|
|
|
|
2009-11-12 17:09:29 +00:00
|
|
|
def session_pool
|
|
|
|
@session_pool ||= {}
|
|
|
|
end
|
2009-11-12 16:51:31 +00:00
|
|
|
end
|
|
|
|
|
2009-11-12 17:09:29 +00:00
|
|
|
extend(self)
|
|
|
|
|
2009-11-14 12:51:52 +00:00
|
|
|
def page
|
2009-11-16 21:02:16 +00:00
|
|
|
Capybara.current_session
|
2009-11-14 12:51:52 +00:00
|
|
|
end
|
2009-11-12 17:09:29 +00:00
|
|
|
|
2009-11-12 16:51:31 +00:00
|
|
|
SESSION_METHODS = [
|
2009-12-16 14:16:52 +00:00
|
|
|
:visit, :current_url, :body, :click_link, :click_button, :drag, :fill_in, :choose, :has_xpath?, :has_css?,
|
2009-11-24 20:32:25 +00:00
|
|
|
:check, :uncheck, :attach_file, :select, :has_content?, :within, :within_fieldset,
|
2009-12-08 18:29:36 +00:00
|
|
|
:within_table, :save_and_open_page, :find, :find_field, :find_link, :find_button,
|
2009-12-20 15:22:47 +00:00
|
|
|
:field_labeled, :all, :wait_for, :wait_for_condition, :evaluate_script, :click
|
2009-11-12 16:51:31 +00:00
|
|
|
]
|
|
|
|
SESSION_METHODS.each do |method|
|
|
|
|
class_eval <<-RUBY, __FILE__, __LINE__+1
|
|
|
|
def #{method}(*args, &block)
|
2009-11-14 12:51:52 +00:00
|
|
|
page.#{method}(*args, &block)
|
2009-11-12 16:51:31 +00:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|