2010-01-12 17:00:01 -05:00
|
|
|
require 'capybara'
|
|
|
|
|
2009-11-16 16:02:16 -05:00
|
|
|
module Capybara
|
2009-11-12 12:09:29 -05:00
|
|
|
class << self
|
2011-01-22 15:19:42 -05:00
|
|
|
attr_writer :default_driver, :current_driver, :javascript_driver, :session_name
|
2009-11-12 11:51:31 -05:00
|
|
|
|
2009-11-12 12:57:46 -05:00
|
|
|
attr_accessor :app
|
|
|
|
|
2010-07-15 15:05:41 -04:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# @return [Symbol] The name of the driver to use by default
|
|
|
|
#
|
2009-11-12 12:09:29 -05:00
|
|
|
def default_driver
|
|
|
|
@default_driver || :rack_test
|
|
|
|
end
|
2009-11-12 11:51:31 -05:00
|
|
|
|
2010-07-15 15:05:41 -04:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# @return [Symbol] The name of the driver currently in use
|
|
|
|
#
|
2009-11-12 12:09:29 -05:00
|
|
|
def current_driver
|
2009-11-26 11:43:39 -05:00
|
|
|
@current_driver || default_driver
|
2009-11-12 12:09:29 -05:00
|
|
|
end
|
|
|
|
alias_method :mode, :current_driver
|
2009-12-12 07:33:00 -05:00
|
|
|
|
2010-07-15 15:05:41 -04:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# @return [Symbol] The name of the driver used when JavaScript is needed
|
|
|
|
#
|
2009-11-16 14:11:42 -05:00
|
|
|
def javascript_driver
|
|
|
|
@javascript_driver || :selenium
|
|
|
|
end
|
2009-11-12 11:51:31 -05:00
|
|
|
|
2010-07-15 15:05:41 -04:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Use the default driver as the current driver
|
|
|
|
#
|
2009-11-12 12:09:29 -05:00
|
|
|
def use_default_driver
|
2009-11-26 11:43:39 -05:00
|
|
|
@current_driver = nil
|
2009-11-12 12:09:29 -05:00
|
|
|
end
|
2009-11-12 11:51:31 -05:00
|
|
|
|
2010-11-07 09:37:33 -05:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Yield a block using a specific driver
|
|
|
|
#
|
|
|
|
def using_driver(driver)
|
|
|
|
Capybara.current_driver = driver
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Capybara.use_default_driver
|
|
|
|
end
|
|
|
|
|
2010-07-15 15:05:41 -04:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# The current Capybara::Session base on what is set as Capybara.app and Capybara.current_driver
|
|
|
|
#
|
|
|
|
# @return [Capybara::Session] The currently used session
|
|
|
|
#
|
2009-11-12 12:57:46 -05:00
|
|
|
def current_session
|
2011-01-28 00:39:36 -05:00
|
|
|
session_pool["#{current_driver}:#{session_name}:#{app.object_id}"] ||= Capybara::Session.new(current_driver, app)
|
2009-11-26 11:43:39 -05:00
|
|
|
end
|
2009-12-12 07:33:00 -05:00
|
|
|
|
2010-07-15 15:05:41 -04:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Reset sessions, cleaning out the pool of sessions. This will remove any session information such
|
|
|
|
# as cookies.
|
|
|
|
#
|
2009-11-15 17:20:37 -05:00
|
|
|
def reset_sessions!
|
2010-07-29 09:25:45 -04:00
|
|
|
session_pool.each { |mode, session| session.reset! }
|
2009-11-15 17:20:37 -05:00
|
|
|
end
|
2010-09-07 12:33:43 -04:00
|
|
|
alias_method :reset!, :reset_sessions!
|
2009-11-25 12:57:32 -05:00
|
|
|
|
2011-01-22 15:19:42 -05:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# The current session name.
|
|
|
|
#
|
|
|
|
# @return [Symbol] The name of the currently used session.
|
|
|
|
#
|
|
|
|
def session_name
|
|
|
|
@session_name ||= :default
|
|
|
|
end
|
|
|
|
|
2011-01-09 17:47:28 -05:00
|
|
|
##
|
|
|
|
#
|
2011-01-22 15:30:37 -05:00
|
|
|
# Yield a block using a specific session name.
|
2011-01-09 17:47:28 -05:00
|
|
|
#
|
2011-01-22 15:30:37 -05:00
|
|
|
def using_session(name)
|
2011-01-22 15:19:42 -05:00
|
|
|
self.session_name = name
|
2011-01-09 17:47:28 -05:00
|
|
|
yield
|
|
|
|
ensure
|
2011-01-22 15:19:42 -05:00
|
|
|
self.session_name = :default
|
2011-01-09 17:47:28 -05:00
|
|
|
end
|
|
|
|
|
2009-11-12 12:57:46 -05:00
|
|
|
private
|
|
|
|
|
2009-11-12 12:09:29 -05:00
|
|
|
def session_pool
|
|
|
|
@session_pool ||= {}
|
|
|
|
end
|
2009-11-12 11:51:31 -05:00
|
|
|
end
|
|
|
|
|
2009-11-12 12:09:29 -05:00
|
|
|
extend(self)
|
|
|
|
|
2011-01-09 17:47:28 -05:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Shortcut to working in a different session. This is useful when Capybara is included
|
|
|
|
# in a class or module.
|
|
|
|
#
|
2011-01-22 15:30:37 -05:00
|
|
|
def using_session(name, &block)
|
|
|
|
Capybara.using_session(name, &block)
|
2011-01-09 17:47:28 -05:00
|
|
|
end
|
|
|
|
|
2010-07-15 15:05:41 -04:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Shortcut to accessing the current session. This is useful when Capybara is included in a
|
|
|
|
# class or module.
|
|
|
|
#
|
|
|
|
# class MyClass
|
|
|
|
# include Capybara
|
|
|
|
#
|
|
|
|
# def has_header?
|
|
|
|
# page.has_css?('h1')
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# @return [Capybara::Session] The current session object
|
|
|
|
#
|
2009-11-14 07:51:52 -05:00
|
|
|
def page
|
2009-11-16 16:02:16 -05:00
|
|
|
Capybara.current_session
|
2009-11-14 07:51:52 -05:00
|
|
|
end
|
2009-11-12 12:09:29 -05:00
|
|
|
|
2009-12-29 22:05:38 -05:00
|
|
|
Session::DSL_METHODS.each do |method|
|
2009-11-12 11:51:31 -05:00
|
|
|
class_eval <<-RUBY, __FILE__, __LINE__+1
|
|
|
|
def #{method}(*args, &block)
|
2009-11-14 07:51:52 -05:00
|
|
|
page.#{method}(*args, &block)
|
2009-11-12 11:51:31 -05:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|