Skip to content

Commit

Permalink
[rb] speed up target locator specs
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Nov 26, 2022
1 parent 50974df commit 71657e9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ def create_driver!(**opts, &block)
GlobalTestEnv.create_driver!(**opts, &block)
end

def ensure_single_window
GlobalTestEnv.ensure_single_window
end

def url_for(filename)
GlobalTestEnv.url_for filename
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,6 @@ def reset_driver!(time = 0)
driver_instance
end

# TODO: optimize since this approach is not assured on IE
def ensure_single_window
driver_instance.window_handles[1..].each do |handle|
driver_instance.switch_to.window(handle)
driver_instance.close
end
driver_instance.switch_to.window(driver_instance.window_handles.first)
end

def quit_driver
return unless @driver_instance

Expand Down
39 changes: 14 additions & 25 deletions rb/spec/integration/selenium/webdriver/target_locator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@
module Selenium
module WebDriver
describe TargetLocator do
before { @original_window = driver.window_handle }

after do
ensure_single_window
handles = driver.window_handles
driver.switch_to.window(@original_window) if handles.include?(@original_window)

(handles - [driver.window_handle]).each do |handle|
driver.switch_to.window(handle) { driver.close }
end
end

let(:new_window) { driver.window_handles.find { |handle| handle != driver.window_handle } }
Expand Down Expand Up @@ -63,26 +70,19 @@ module WebDriver
end

context 'window switching' do
after do
sleep 1 if ENV['TRAVIS']
quit_driver
end

describe '#new_window' do
it 'should switch to a new window' do
original_window = driver.window_handle
driver.switch_to.new_window(:window)

expect(driver.window_handles.size).to eq 2
expect(driver.window_handle).not_to eq original_window
expect(driver.window_handle).not_to eq @original_window
end

it 'should switch to a new tab' do
original_window = driver.window_handle
driver.switch_to.new_window(:tab)

expect(driver.window_handles.size).to eq 2
expect(driver.window_handle).not_to eq original_window
expect(driver.window_handle).not_to eq @original_window
end

it 'should raise exception when the new window type is not recognized' do
Expand All @@ -92,14 +92,12 @@ module WebDriver
end

it 'should switch to the new window then close it when given a block' do
original_window = driver.window_handle

driver.switch_to.new_window do
expect(driver.window_handles.size).to eq 2
end

expect(driver.window_handles.size).to eq 1
expect(driver.window_handle).to eq original_window
expect(driver.window_handle).to eq @original_window
end

it 'should not error if switching to a new window with a block that closes window' do
Expand Down Expand Up @@ -168,29 +166,20 @@ module WebDriver

context 'with more than two windows', except: [{browser: %i[safari safari_preview]},
{driver: :remote, browser: :ie}] do
after do
# We need to reset driver because browsers behave differently
# when trying to open the same blank target in a new window.
# Sometimes it's opened in a new window (Firefox 55), sometimes
# in the same window (Firefox 57). In any event, this has nothing
# to do with Selenium test.
sleep 1 if ENV['TRAVIS']
reset_driver!
end

it 'should close current window when more than two windows exist' do
it 'should close current window via block' do
driver.navigate.to url_for('xhtmlTest.html')
wait_for_element(link: 'Create a new anonymous window')
driver.find_element(link: 'Create a new anonymous window').click
wait.until { driver.window_handles.size == 2 }
driver.find_element(link: 'Open new window').click
wait.until { driver.window_handles.size == 3 }
driver.switch_to.window(new_window)

driver.switch_to.window(driver.window_handle) { driver.close }
expect(driver.window_handles.size).to eq 2
end

it 'should close another window when more than two windows exist' do
it 'should close another window' do
driver.navigate.to url_for('xhtmlTest.html')
wait_for_element(link: 'Create a new anonymous window')
driver.find_element(link: 'Create a new anonymous window').click
Expand Down

0 comments on commit 71657e9

Please sign in to comment.