2016-03-07 19:52:19 -05:00
|
|
|
# frozen_string_literal: true
|
2014-04-08 17:28:16 -04:00
|
|
|
Capybara::SpecHelper.spec '#window_opened_by', requires: [:windows] do
|
|
|
|
before(:each) do
|
|
|
|
@window = @session.current_window
|
|
|
|
@session.visit('/with_windows')
|
2016-07-27 18:09:25 -04:00
|
|
|
expect(@session).to have_css('body.loaded')
|
2014-04-08 17:28:16 -04:00
|
|
|
end
|
|
|
|
after(:each) do
|
|
|
|
(@session.windows - [@window]).each do |w|
|
|
|
|
@session.switch_to_window w
|
|
|
|
w.close
|
|
|
|
end
|
|
|
|
@session.switch_to_window(@window)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:zero_windows_message) { "block passed to #window_opened_by opened 0 windows instead of 1" }
|
|
|
|
let(:two_windows_message) { "block passed to #window_opened_by opened 2 windows instead of 1" }
|
|
|
|
|
|
|
|
context 'with :wait option' do
|
|
|
|
it 'should raise error if value of :wait is less than timeout' do
|
2014-08-24 07:54:40 -04:00
|
|
|
# So large value is used as `driver.window_handles` takes up to 800 ms on Travis
|
|
|
|
Capybara.using_wait_time 2 do
|
2015-08-25 20:15:44 -04:00
|
|
|
button=@session.find(:css, '#openWindowWithLongerTimeout')
|
2014-04-08 17:28:16 -04:00
|
|
|
expect do
|
2017-05-10 15:27:53 -04:00
|
|
|
@session.window_opened_by(wait: 0.3) do
|
2015-08-25 20:15:44 -04:00
|
|
|
button.click
|
2014-04-08 17:28:16 -04:00
|
|
|
end
|
|
|
|
end.to raise_error(Capybara::WindowError, zero_windows_message)
|
|
|
|
end
|
2017-05-10 15:27:53 -04:00
|
|
|
@session.document.synchronize(5, errors: [Capybara::CapybaraError]) do
|
2014-05-28 15:34:31 -04:00
|
|
|
raise Capybara::CapybaraError if @session.windows.size != 2
|
|
|
|
end
|
2014-04-08 17:28:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should find window if value of :wait is more than timeout' do
|
2015-08-25 20:15:44 -04:00
|
|
|
button = @session.find(:css, '#openWindowWithTimeout')
|
2014-04-08 17:28:16 -04:00
|
|
|
Capybara.using_wait_time 0.1 do
|
2014-04-24 18:38:42 -04:00
|
|
|
window = @session.window_opened_by(wait: 1.5) do
|
2015-08-25 20:15:44 -04:00
|
|
|
button.click
|
2014-04-08 17:28:16 -04:00
|
|
|
end
|
|
|
|
expect(window).to be_instance_of(Capybara::Window)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without :wait option' do
|
2015-04-14 16:56:30 -04:00
|
|
|
it 'should raise error if default_max_wait_time is less than timeout' do
|
2015-08-25 20:14:14 -04:00
|
|
|
button = @session.find(:css, '#openWindowWithTimeout')
|
2017-06-07 14:16:29 -04:00
|
|
|
Capybara.using_wait_time 0.1 do
|
2014-04-08 17:28:16 -04:00
|
|
|
expect do
|
|
|
|
@session.window_opened_by do
|
2015-08-25 20:14:14 -04:00
|
|
|
button.click
|
2014-04-08 17:28:16 -04:00
|
|
|
end
|
|
|
|
end.to raise_error(Capybara::WindowError, zero_windows_message)
|
|
|
|
end
|
2014-05-28 15:34:31 -04:00
|
|
|
@session.document.synchronize(2, errors: [Capybara::CapybaraError]) do
|
|
|
|
raise Capybara::CapybaraError if @session.windows.size != 2
|
|
|
|
end
|
2014-04-08 17:28:16 -04:00
|
|
|
end
|
|
|
|
|
2015-04-14 16:56:30 -04:00
|
|
|
it 'should find window if default_max_wait_time is more than timeout' do
|
2015-08-25 20:14:14 -04:00
|
|
|
button = @session.find(:css, '#openWindowWithTimeout')
|
2017-05-10 15:27:53 -04:00
|
|
|
Capybara.using_wait_time 5 do
|
2014-04-08 17:28:16 -04:00
|
|
|
window = @session.window_opened_by do
|
2015-08-25 20:14:14 -04:00
|
|
|
button.click
|
2014-04-08 17:28:16 -04:00
|
|
|
end
|
|
|
|
expect(window).to be_instance_of(Capybara::Window)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should raise error when two windows have been opened by block' do
|
2015-08-25 20:14:14 -04:00
|
|
|
button = @session.find(:css, '#openTwoWindows')
|
2014-04-08 17:28:16 -04:00
|
|
|
expect do
|
|
|
|
@session.window_opened_by do
|
2015-08-25 20:14:14 -04:00
|
|
|
button.click
|
2016-07-27 18:09:25 -04:00
|
|
|
sleep 1 # It's possible for window_opened_by to be fullfilled before the second window opens
|
2014-04-08 17:28:16 -04:00
|
|
|
end
|
|
|
|
end.to raise_error(Capybara::WindowError, two_windows_message)
|
2014-05-28 15:34:31 -04:00
|
|
|
@session.document.synchronize(2, errors: [Capybara::CapybaraError]) do
|
|
|
|
raise Capybara::CapybaraError if @session.windows.size != 3
|
|
|
|
end
|
2014-04-08 17:28:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should raise error when no windows were opened by block' do
|
2015-08-25 20:14:14 -04:00
|
|
|
button = @session.find(:css, '#doesNotOpenWindows')
|
2014-04-08 17:28:16 -04:00
|
|
|
expect do
|
|
|
|
@session.window_opened_by do
|
2015-08-25 20:14:14 -04:00
|
|
|
button.click
|
2014-04-08 17:28:16 -04:00
|
|
|
end
|
|
|
|
end.to raise_error(Capybara::WindowError, zero_windows_message)
|
|
|
|
end
|
|
|
|
end
|