Allow `using_session` to accept a name or Session object

This commit is contained in:
Thomas Walpole 2018-10-01 11:58:27 -07:00
parent edc2859376
commit 29853e4fad
3 changed files with 42 additions and 12 deletions

View File

@ -299,7 +299,7 @@ module Capybara
# @return [Capybara::Session] The currently used session # @return [Capybara::Session] The currently used session
# #
def current_session def current_session
session_pool["#{current_driver}:#{session_name}:#{app.object_id}"] ||= Capybara::Session.new(current_driver, app) specified_session || session_pool["#{current_driver}:#{session_name}:#{app.object_id}"]
end end
## ##
@ -337,22 +337,25 @@ module Capybara
## ##
# #
# Yield a block using a specific session name. # Yield a block using a specific session name or Capybara::Session instance.
# #
def using_session(name) def using_session(name_or_session)
previous_session_info = { previous_session_info = {
specified_session: specified_session,
session_name: session_name, session_name: session_name,
current_driver: current_driver, current_driver: current_driver,
app: app app: app
} }
self.session_name = name self.specified_session = self.session_name = nil
if name_or_session.is_a? Capybara::Session
self.specified_session = name_or_session
else
self.session_name = name_or_session
end
yield yield
ensure ensure
self.session_name = previous_session_info[:session_name] self.session_name, self.specified_session = previous_session_info.values_at(:session_name, :specified_session)
if threadsafe self.current_driver, self.app = previous_session_info.values_at(:current_driver, :app) if threadsafe
self.current_driver = previous_session_info[:current_driver]
self.app = previous_session_info[:app]
end
end end
## ##
@ -381,7 +384,25 @@ module Capybara
end end
def session_pool def session_pool
@session_pool ||= {} @session_pool ||= Hash.new do |hash, name|
hash[name] = Capybara::Session.new(current_driver, app)
end
end
def specified_session
if threadsafe
Thread.current['capybara_specified_session']
else
@specified_session
end
end
def specified_session=(session)
if threadsafe
Thread.current['capybara_specified_session'] = session
else
@specified_session = session
end
end end
end end

View File

@ -18,8 +18,8 @@ module Capybara
# #
# Shortcut to working in a different session. # Shortcut to working in a different session.
# #
def using_session(name, &block) def using_session(name_or_session, &block)
Capybara.using_session(name, &block) Capybara.using_session(name_or_session, &block)
end end
# Shortcut to using a different wait time. # Shortcut to using a different wait time.

View File

@ -224,6 +224,15 @@ RSpec.describe Capybara::DSL do
end end
expect(Capybara.session_name).to eq(:default) expect(Capybara.session_name).to eq(:default)
end end
it 'should allow a session object' do
original_session = Capybara.current_session
new_session = Capybara::Session.new(:rack_test, proc {})
Capybara.using_session(new_session) do
expect(Capybara.current_session).to eq(new_session)
end
expect(Capybara.current_session).to eq(original_session)
end
end end
describe '#session_name' do describe '#session_name' do