Reputation: 6053
[7] pry(#<RSpec::Core::ExampleGroup::Nested_1>)> page.execute_script "window.close()"
Selenium::WebDriver::Error::NoSuchWindowError: Script execution failed. Script: window.close();
The window could not be found
[8] pry(#<RSpec::Core::ExampleGroup::Nested_1>)> page.driver.browser.window_handles
=> ["f1-2"]
I had a browser open with two tabs, the above command does close one but the last tab never closes. It is open but when I try to run page.execute_script "window.close()"
it gives the above error.
page.driver.browser.window_handles.each do |handle|
page.driver.browser.switch_to.window(handle)
page.execute_script "window.close()"
end
The above code was working for me sometime back but doesnt work anymore. It gives the same error.
UPDATE:
When I use,
page.driver.browser.window_handles.each do |handle|
page.driver.browser.switch_to.window(handle)
page.driver.browser.close
end
it gives the following error Selenium::WebDriver::Error::UnknownError: 'auto_id'
does not refer to an open tab
Upvotes: 2
Views: 2744
Reputation: 6053
page.driver.browser.close
closes current tab towards the end and the last (second) tab closes itself after each example.
Upvotes: 1
Reputation:
in case if you are using cucumber, you can the use the BEFORE/AFTER
hooks .please refer similar question on stackoverflow
for some more on cucumber,please refer this Cucumber Hooks
Upvotes: 0
Reputation: 8548
Two ways you can do it
In line with your technique using JS. You would first need to switch back to your first browser window (window_handle) and then perform "window.close()". (Not Preferred) (Not sure why its not working now for you, did you upgrade server version or different browser?)
Simply use @driver.quit
(Preferred)
Update
Just write this once. This will close all windows.
after(:each) do
@driver.quit
end
If you want to close only one browser tab/window/popup, switch to that window_handle and then perform
@driver.close();
Upvotes: 1