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/dsl.rb

140 lines
2.8 KiB
Ruby
Raw Normal View History

require 'capybara'
2009-11-16 16:02:16 -05:00
module Capybara
2009-11-12 12:09:29 -05:00
class << self
attr_writer :default_driver, :current_driver, :javascript_driver, :session_name
2009-11-12 11:51:31 -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
@current_driver || default_driver
2009-11-12 12:09:29 -05:00
end
alias_method :mode, :current_driver
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
@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
#
def current_session
session_pool["#{current_driver}:#{session_name}:#{app.object_id}"] ||= Capybara::Session.new(current_driver, app)
end
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
alias_method :reset!, :reset_sessions!
##
#
# The current session name.
#
# @return [Symbol] The name of the currently used session.
#
def session_name
@session_name ||= :default
end
##
#
# Yield a block using a specific session name.
#
def using_session(name)
self.session_name = name
yield
ensure
self.session_name = :default
end
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)
##
#
# Shortcut to working in a different session. This is useful when Capybara is included
# in a class or module.
#
def using_session(name, &block)
Capybara.using_session(name, &block)
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
#
def page
2009-11-16 16:02:16 -05:00
Capybara.current_session
end
2009-11-12 12:09:29 -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)
page.#{method}(*args, &block)
2009-11-12 11:51:31 -05:00
end
RUBY
end
end