diff --git a/lib/bucky/test_equipment/user_operation/user_operation_helper.rb b/lib/bucky/test_equipment/user_operation/user_operation_helper.rb index 134cc40..bda5860 100644 --- a/lib/bucky/test_equipment/user_operation/user_operation_helper.rb +++ b/lib/bucky/test_equipment/user_operation/user_operation_helper.rb @@ -46,11 +46,24 @@ def refresh(_) @driver.navigate.refresh end - def switch_next_window(_) + def switch_to_next_window(_) + window_index = @driver.window_handles.index(@driver.window_handle) + windows_number = @driver.window_handles.size + unless window_index+1 == windows_number + @driver.switch_to.window(@driver.window_handles[window_index+1]) + end + end + + def switch_to_previous_window(_) + window_index = @driver.window_handles.index(@driver.window_handle) + @driver.switch_to.window(@driver.window_handles[window_index-1]) + end + + def switch_to_newest_window(_) @driver.switch_to.window(@driver.window_handles.last) end - def back_to_window(_) + def switch_to_oldest_window(_) @driver.switch_to.window(@driver.window_handles.first) end @@ -61,11 +74,13 @@ def switch_to_the_window(args) # Close window def close(_) + window_index = @driver.window_handles.index(@driver.window_handle) @driver.close + @driver.switch_to.window(@driver.window_handles[window_index-1]) end def stop(_) - puts 'stop. please enter to continue' + puts 'stop. press enter to continue' gets end diff --git a/spec/test_equipment/user_operation/user_operation_helper_spec.rb b/spec/test_equipment/user_operation/user_operation_helper_spec.rb index 6fe0086..295de08 100644 --- a/spec/test_equipment/user_operation/user_operation_helper_spec.rb +++ b/spec/test_equipment/user_operation/user_operation_helper_spec.rb @@ -35,20 +35,62 @@ end end - describe '#switch_next_window' do - let(:operation) { :switch_next_window } + describe '#switch_to_next_window' do + let(:operation) { :switch_to_next_window } + let(:window_handles_double) { double('window_handles double') } + let(:window_index) { 2 } + let(:windows_number) { 4 } + before do + allow(driver_double).to receive(:window_handle) + allow(driver_double).to receive_message_chain(:window_handles, :index).and_return(window_index) + allow(driver_double).to receive_message_chain(:window_handles, :size).and_return(windows_number) + end + it 'call driver.switch_to.window' do + allow(driver_double).to receive_message_chain(:window_handles, :[]) + expect(driver_double).to receive_message_chain(:switch_to, :window) + subject.send(operation, nil) + end + + it 'switch to next window index' do + allow(driver_double).to receive_message_chain(:switch_to, :window) + expect(driver_double).to receive_message_chain(:window_handles, :[]).with(window_index + 1) + subject.send(operation, nil) + end + end + + describe '#switch_to_previous_window' do + let(:operation) { :switch_to_previous_window } + let(:window_index) { 2 } + before do + allow(driver_double).to receive(:window_handle) + allow(driver_double).to receive_message_chain(:window_handles, :index).and_return(window_index) + end + it 'call driver.switch_to.window' do + allow(driver_double).to receive_message_chain(:window_handles, :[]) + expect(driver_double).to receive_message_chain(:switch_to, :window) + subject.send(operation, nil) + end + + it 'switch to previous window index' do + allow(driver_double).to receive_message_chain(:switch_to, :window) + expect(driver_double).to receive_message_chain(:window_handles, :[]).with(window_index - 1) + subject.send(operation, nil) + end + end + + describe '#switch_to_newest_window' do + let(:operation) { :switch_to_newest_window } before do allow(driver_double).to receive_message_chain(:window_handles, :last) end it 'call driver.switch_to.window' do - allow(driver_double).to receive_message_chain(:switch_to, :frame) expect(driver_double).to receive_message_chain(:switch_to, :window) subject.send(operation, nil) end end - describe '#back_to_window' do - let(:operation) { :back_to_window } + describe '#switch_to_oldest_window' do + let(:operation) { :switch_to_oldest_window } before do allow(driver_double).to receive_message_chain(:window_handles, :first) end @@ -73,8 +115,22 @@ describe '#close' do let(:operation) { :close } + let(:window_index) { 2 } + before do + allow(driver_double).to receive(:window_handle) + allow(driver_double).to receive_message_chain(:window_handles, :index).and_return(window_index) + end it 'call driver.close' do - expect(driver_double).to receive_message_chain(:close) + allow(driver_double).to receive_message_chain(:window_handles, :[]) + allow(driver_double).to receive_message_chain(:switch_to, :window) + expect(driver_double).to receive(:close) + subject.send(operation, nil) + end + + it 'switch to last window index' do + allow(driver_double).to receive(:close) + allow(driver_double).to receive_message_chain(:switch_to, :window) + expect(driver_double).to receive_message_chain(:window_handles, :[]).with(window_index - 1) subject.send(operation, nil) end end