thesis
thesis

Reputation: 2565

How to close browser with Capybara?

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?

Upvotes: 14

Views: 10976

Answers (4)

ConorSheehan1
ConorSheehan1

Reputation: 1725

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

Upvotes: 8

lobati
lobati

Reputation: 10195

Also:

page.driver.browser.close

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

Upvotes: 8

pedroconsuegrat
pedroconsuegrat

Reputation: 71

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

Upvotes: 1

Wojtek Kruszewski
Wojtek Kruszewski

Reputation: 14720

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();"

Upvotes: 8

Related Questions