2016-03-08 00:52:19 +00:00
|
|
|
# frozen_string_literal: true
|
2018-03-01 00:11:41 +00:00
|
|
|
|
2020-11-26 20:16:11 +00:00
|
|
|
# NOTE: This file uses `sleep` to sync up parts of the tests. This is only implemented like this
|
2018-10-09 17:29:25 +00:00
|
|
|
# because of the methods being tested. In tests using Capybara this type of behavior should be implemented
|
|
|
|
# using Capybara provided assertions with builtin waiting behavior.
|
|
|
|
|
2014-04-08 21:28:16 +00:00
|
|
|
Capybara::SpecHelper.spec '#switch_to_window', requires: [:windows] do
|
2018-04-27 18:01:47 +00:00
|
|
|
before do
|
2014-04-08 21:28:16 +00:00
|
|
|
@window = @session.current_window
|
|
|
|
@session.visit('/with_windows')
|
2018-04-27 18:01:47 +00:00
|
|
|
@session.assert_selector(:css, 'body.loaded')
|
2014-04-08 21:28:16 +00:00
|
|
|
end
|
2016-07-27 22:09:25 +00:00
|
|
|
|
2018-04-27 18:01:47 +00:00
|
|
|
after do
|
2014-04-08 21:28:16 +00:00
|
|
|
(@session.windows - [@window]).each do |w|
|
|
|
|
@session.switch_to_window w
|
|
|
|
w.close
|
|
|
|
end
|
|
|
|
@session.switch_to_window(@window)
|
|
|
|
end
|
|
|
|
|
2018-07-10 21:18:39 +00:00
|
|
|
it 'should raise error when invoked without args' do
|
2014-04-08 21:28:16 +00:00
|
|
|
expect do
|
|
|
|
@session.switch_to_window
|
2018-07-10 21:18:39 +00:00
|
|
|
end.to raise_error(ArgumentError, '`switch_to_window`: either window or block should be provided')
|
2014-04-08 21:28:16 +00:00
|
|
|
end
|
|
|
|
|
2018-07-10 21:18:39 +00:00
|
|
|
it 'should raise error when invoked with window and block' do
|
2014-04-08 21:28:16 +00:00
|
|
|
expect do
|
|
|
|
@session.switch_to_window(@window) { @session.title == 'Title of the first popup' }
|
2018-07-10 21:18:39 +00:00
|
|
|
end.to raise_error(ArgumentError, '`switch_to_window` can take either a block or a window, not both')
|
2014-04-08 21:28:16 +00:00
|
|
|
end
|
|
|
|
|
2018-07-10 21:18:39 +00:00
|
|
|
context 'with an instance of Capybara::Window' do
|
|
|
|
it 'should be able to switch to window' do
|
2014-04-08 21:28:16 +00:00
|
|
|
window = @session.open_new_window
|
|
|
|
expect(@session.title).to eq('With Windows')
|
|
|
|
@session.switch_to_window(window)
|
2018-04-27 18:01:47 +00:00
|
|
|
expect(@session.title).to satisfy('be a blank title') { |title| ['', 'about:blank'].include? title }
|
2014-04-08 21:28:16 +00:00
|
|
|
end
|
2014-04-09 08:22:17 +00:00
|
|
|
|
2018-07-10 21:18:39 +00:00
|
|
|
it 'should raise error when closed window is passed' do
|
2014-04-09 08:22:17 +00:00
|
|
|
original_window = @session.current_window
|
|
|
|
new_window = @session.open_new_window
|
|
|
|
@session.switch_to_window(new_window)
|
|
|
|
new_window.close
|
|
|
|
@session.switch_to_window(original_window)
|
|
|
|
expect do
|
|
|
|
@session.switch_to_window(new_window)
|
|
|
|
end.to raise_error(@session.driver.no_such_window_error)
|
|
|
|
end
|
2014-04-08 21:28:16 +00:00
|
|
|
end
|
|
|
|
|
2018-07-10 21:18:39 +00:00
|
|
|
context 'with block' do
|
2018-04-27 18:01:47 +00:00
|
|
|
before do
|
2014-04-08 21:28:16 +00:00
|
|
|
@session.find(:css, '#openTwoWindows').click
|
2016-07-27 22:09:25 +00:00
|
|
|
sleep(1) # wait for the windows to open
|
2014-04-08 21:28:16 +00:00
|
|
|
end
|
|
|
|
|
2018-07-10 21:18:39 +00:00
|
|
|
it 'should be able to switch to current window' do
|
2014-04-08 21:28:16 +00:00
|
|
|
@session.switch_to_window { @session.title == 'With Windows' }
|
|
|
|
expect(@session).to have_css('#openTwoWindows')
|
|
|
|
end
|
|
|
|
|
2018-07-10 21:18:39 +00:00
|
|
|
it 'should find the div in another window' do
|
2014-04-08 21:28:16 +00:00
|
|
|
@session.switch_to_window { @session.title == 'Title of popup two' }
|
|
|
|
expect(@session).to have_css('#divInPopupTwo')
|
|
|
|
end
|
|
|
|
|
2018-07-10 21:18:39 +00:00
|
|
|
it 'should be able to switch multiple times' do
|
2014-04-08 21:28:16 +00:00
|
|
|
@session.switch_to_window { @session.title == 'Title of the first popup' }
|
|
|
|
expect(@session).to have_css('#divInPopupOne')
|
|
|
|
@session.switch_to_window { @session.title == 'Title of popup two' }
|
|
|
|
expect(@session).to have_css('#divInPopupTwo')
|
|
|
|
end
|
|
|
|
|
2018-07-10 21:18:39 +00:00
|
|
|
it 'should return window' do
|
2014-04-08 21:28:16 +00:00
|
|
|
window = @session.switch_to_window { @session.title == 'Title of popup two' }
|
|
|
|
expect((@session.windows - [@window])).to include(window)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise error when invoked inside `within` as it's nonsense" do
|
|
|
|
expect do
|
|
|
|
@session.within(:css, '#doesNotOpenWindows') do
|
|
|
|
@session.switch_to_window { @session.title == 'With Windows' }
|
|
|
|
end
|
2017-06-23 00:21:24 +00:00
|
|
|
end.to raise_error(Capybara::ScopeError, /`switch_to_window` is not supposed to be invoked/)
|
2014-04-08 21:28:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise error when invoked inside `within_frame` as it's nonsense" do
|
|
|
|
expect do
|
|
|
|
@session.within_frame('frameOne') do
|
|
|
|
@session.switch_to_window { @session.title == 'With Windows' }
|
|
|
|
end
|
2017-06-23 00:21:24 +00:00
|
|
|
end.to raise_error(Capybara::ScopeError, /`switch_to_window` is not supposed to be invoked from/)
|
2014-04-08 21:28:16 +00:00
|
|
|
end
|
|
|
|
|
2018-07-10 21:18:39 +00:00
|
|
|
it 'should allow to be called inside within_window and within_window will still return to original' do
|
2017-06-23 00:21:24 +00:00
|
|
|
other_windows = (@session.windows - [@window])
|
|
|
|
expect(@session.current_window).to eq(@window)
|
|
|
|
@session.within_window other_windows[0] do
|
|
|
|
expect(@session.current_window).to eq(other_windows[0])
|
|
|
|
@session.switch_to_window other_windows[1]
|
|
|
|
expect(@session.current_window).to eq(other_windows[1])
|
|
|
|
end
|
|
|
|
expect(@session.current_window).to eq(@window)
|
2014-04-08 21:28:16 +00:00
|
|
|
end
|
|
|
|
|
2014-04-09 08:22:17 +00:00
|
|
|
it "should raise error if window matching block wasn't found" do
|
|
|
|
original = @session.current_window
|
|
|
|
expect do
|
|
|
|
@session.switch_to_window { @session.title == 'A title' }
|
2018-07-10 21:18:39 +00:00
|
|
|
end.to raise_error(Capybara::WindowError, 'Could not find a window matching block/lambda')
|
2014-04-09 08:22:17 +00:00
|
|
|
expect(@session.current_window).to eq(original)
|
|
|
|
end
|
|
|
|
|
2018-07-10 21:18:39 +00:00
|
|
|
it 'should switch to original window if error is raised inside block' do
|
2014-04-09 08:22:17 +00:00
|
|
|
original = @session.switch_to_window(@session.windows[1])
|
|
|
|
expect do
|
|
|
|
@session.switch_to_window { raise 'error' }
|
|
|
|
end.to raise_error(StandardError, 'error')
|
|
|
|
expect(@session.current_window).to eq(original)
|
|
|
|
end
|
2014-04-08 21:28:16 +00:00
|
|
|
end
|
2015-07-22 02:16:52 +00:00
|
|
|
|
2018-07-10 21:18:39 +00:00
|
|
|
it 'should wait for window to appear' do
|
2015-07-22 02:16:52 +00:00
|
|
|
@session.find(:css, '#openWindowWithTimeout').click
|
|
|
|
expect do
|
2018-03-01 00:11:41 +00:00
|
|
|
@session.switch_to_window(wait: 5) { @session.title == 'Title of the first popup' }
|
2015-08-22 00:46:05 +00:00
|
|
|
end.not_to raise_error
|
2015-07-22 02:16:52 +00:00
|
|
|
end
|
2014-04-08 21:28:16 +00:00
|
|
|
end
|