How to close browser with Capybara?

2019-04-19 07:02发布

问题:

I have a situation when I'm using (using :selenium driver) multiple browsers with Capybara to test my front-end. How can I close some of them using Capybara, when they are not needed?

回答1:

If the tabs/windows are opened using JavaScript, then JavaScript is allowed to close them. You can execute JS in Capybara test using page.execute_script.

page.execute_script "window.close();"


回答2:

Also:

page.driver.browser.close

Definitely works for Selenium, though it looks like possibly not for capybara-webkit.



回答3:

If you want to close each window after each test finishes rather than closing all the windows at the end of your test suit, try:

after do  
  Capybara.current_session.driver.quit  
end


回答4:

Inside the env.rb file insert this code

After do |scenario| 
print "OZONEEEEE"
page.execute_script "window.close();"
# restart Selenium driver
Capybara.send(:session_pool).delete_if { |key, value| key =~ /selenium/i } 
end