Reputation: 193
So when I run my tests individually, they work great. However, when I run my feature (multiple tests) my code is failing.
This is because when capybara exists, not all of my windows are closing and therefore my selenium drivers don't know which window to use.
Basically, what is happening is that my test is opening multiple windows. This is happening because my tests are clicking links which open new windows and checking the content in the new windows. Even though I am saying page.quit
at the end of each test, this function isn't closing all open windows (closes active window but not the original window). When the 2nd test runs, it uses the originally create window, but when it goes to open the new pop up, it uses the wrong window.
How can I ensure that a new browser is being used during each test and all windows from the previous test are closed.
I am currently using page.reset!
and page.quit
.
Not sure, but can I say session.quit
to close all open browser windows?
Upvotes: 8
Views: 9231
Reputation: 2789
Adding this here because this page comes up within the first few Google results for "capybara close all windows".
We had some mysterious feature spec failures that we tracked down to a few that opened app links in second tabs -- since the tabs were just left open, every so often they'd fire a failing AJAX request and the current test would bomb out with a completely unrelated error. The way we fixed that was to add a hook that made sure to close all but the current window after every feature spec:
Rspec.configure do |config|
config.after(type: :feature) do
# Make sure all browser windows except one are closed
windows.reject(&:current?).each(&:close)
end
end
This worked like a charm for us, using Capybara's headless_chrome
driver.
Upvotes: 1
Reputation: 1653
In order to have all assertions in spec file, I passed control to new page (window), after executing assertions I closed new page and passed control to original page as bellow:
after(:each) do
expect(@static_page.has_logo?).to eq(true) # assertion in new page
page.execute_script('window.close()') # close new page
switch_to_window(windows.first) # return control to first page
end
Upvotes: 1
Reputation: 4434
Add this to your feature tests, it'll reset the session cookies and start the test on a blank page:
# Window washing - use a clean window before every example
before(:example) do
page.reset!
end
Upvotes: 0
Reputation: 11
Try this:
tab_id = page.driver.find_window("http://google.com.au")
page.driver.browser.switch_to.window(tab_id)
page.driver.quit
This worked for me.
Upvotes: 1
Reputation: 6043
page.driver.browser.window_handles.each do |handle|
page.driver.browser.switch_to.window(handle)
page.execute_script "window.close()"
end
Did the charm for me. Combination of @Jason and @Justin's answers.
Thanks!
Upvotes: 4
Reputation: 193
This code works:
page.execute_script "window.close();"
I just execute this while in the window I want to close.
Upvotes: 8
Reputation: 1068
I use this code piece to access popups and close them after. It should not be much difference
within_window(page.driver.browser.window_handles.last) do
click buttons and stuff...
...
page.driver.browser.close #closes popup
end
Hope it helps
Upvotes: 4
Reputation: 46836
Try the following:
page.driver.browser.window_handles.each do |handle|
page.driver.browser.switch_to.window(handle)
page.quit
end
(I think it will work, but I have not had a chance to test it.)
Upvotes: 1