Skip to content

Commit

Permalink
Merge a9895ac into 49f8917
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-voronenko committed Apr 17, 2019
2 parents 49f8917 + a9895ac commit f14048a
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
39 changes: 39 additions & 0 deletions lib/howitzer.rb
Expand Up @@ -29,6 +29,45 @@ def mailgun_idle_timeout
::SexySettings::Base.instance.all['mailgun_idle_timeout']
end

# @return active session name

def session_name
@session_name ||= 'default'
end

# Sets new session name
#
# @param name [String] string identifier for the session
#
# @example Executing code in another browser
# Howitzer.session_name = 'browser2'
# LoginPage.on do
# expect(title).to eq('Login Page')
# end
#
# # Switching back to main browser
# Howitzer.session_name = 'default'

def session_name=(name)
@session_name = name
Capybara.session_name = @session_name
end

# Yield a block using a specific session name
#
# @param name [String] string identifier for the session
#
# @example Opening page in another browser
# Howitzer.using_session('browser2') do
# LoginPage.on do
# expect(title).to eq('Login Page')
# end
# end

def using_session(name)
Capybara.using_session(name) { yield }
end

attr_accessor :current_rake_task
end

Expand Down
3 changes: 2 additions & 1 deletion lib/howitzer/web/capybara_methods_proxy.rb
Expand Up @@ -59,7 +59,8 @@ def click_alert_box(flag)
private

def capybara_scopes
@capybara_scopes ||= [Capybara.current_session]
@capybara_scopes ||= Hash.new { |hash, key| hash[key] = [Capybara.current_session] }
@capybara_scopes[Howitzer.session_name]
end
end
end
Expand Down
23 changes: 23 additions & 0 deletions spec/unit/lib/howitzer_spec.rb
Expand Up @@ -66,4 +66,27 @@
end
it { is_expected.to be_nil }
end
describe '.session_name' do
context 'when default' do
subject { Howitzer.session_name }
it do
is_expected.to be_eql('default')
end
end
context 'when set' do
subject { Howitzer.session_name = 'another' }
it do
expect(Capybara).to receive(:session_name=).with('another')
is_expected.to be_eql('another')
end
end
end
describe 'using_session' do
before { Howitzer.session_name = 'default' }
it do
expect(Capybara).to receive(:using_session).with('another')
Howitzer.using_session('another') {}
expect(Howitzer.session_name).to be_eql('default')
end
end
end
6 changes: 4 additions & 2 deletions spec/unit/lib/meta/entry_spec.rb
Expand Up @@ -6,7 +6,8 @@
Class.new do
include Howitzer::Web::ElementDsl
def capybara_scopes
@capybara_scopes ||= [Capybara.current_session]
@capybara_scopes ||= Hash.new { |hash, key| hash[key] = [Capybara.current_session] }
@capybara_scopes[Howitzer.session_name]
end

def foo_section; end
Expand Down Expand Up @@ -65,7 +66,8 @@ def has_bar_iframe?; end

describe '#iframes' do
subject { described_class.new(klass.new).iframes }
it { expect(subject.map(&:name)).to eq(%w[foo bar]) }
it { expect(subject.map(&:name)).to contain_exactly('foo', 'bar') }
it { expect(subject.count).to eq(2) }
end

describe '#iframe' do
Expand Down
11 changes: 10 additions & 1 deletion spec/unit/lib/web/element_dsl_spec.rb
Expand Up @@ -6,7 +6,8 @@
Class.new do
include Howitzer::Web::ElementDsl
def capybara_scopes
@capybara_scopes ||= [Capybara.current_session]
@capybara_scopes ||= Hash.new { |hash, key| hash[key] = [Capybara.current_session] }
@capybara_scopes[Howitzer.session_name]
end
end
end
Expand All @@ -17,6 +18,14 @@ def capybara_scopes
expect(klass_object.capybara_context).to eq('session')
end

it 'returns another capybara context' do
allow(Capybara).to receive(:current_session) { 'session' }
expect(klass_object.capybara_context).to eq('session')
Howitzer.session_name = 'session2'
allow(Capybara).to receive(:current_session) { 'session2' }
expect(klass_object.capybara_context).to eq('session2')
end

include_examples :element_dsl
include_examples :capybara_context_holder
end

0 comments on commit f14048a

Please sign in to comment.