0

I am having some issues in Jenkins. After test execution completes, the browser session is still alive, which is creating some other issue in the pipeline. So I want to quit the browser session after execution completed in my Test Suite. I am using Cucumber>Capybara>SitePrism>Ruby.

How I can do that? I want something like below which will execute after very end of my test suite:

RSpec.configure do |config|
  config.after(:suite) do
    puts 'Destroy Driver'
  end
end
Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
SaeeK
  • 217
  • 1
  • 4
  • 12

3 Answers3

2

You could try

Capybara.send(:session_pool).each { |name, ses| ses.driver.quit }

which should call quit on every instance of Selenium::Webdriver, however that is accessing the private method sesion_pool, and really those instances should just automatically get cleaned up when capybara exits unless you're doing something strange with sessions (or maybe there's a bug in selenium when used with phantomjs).

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
  • Got this below Error: undefined method `quit' for ["selenium:default:8", #]:Array (NoMethodError) – SaeeK Jan 28 '16 at 20:03
2

This answer is for folks coming here from their internet searches.

Use the quit instance method defined by Capybara::Session.

I found two ways to hit this:

  1. If you're using Capybara::DSL directly (e.g. in a plain ruby script like I am), current_scope.session.quit does the trick.
  2. Or, Capybara.current_session.quit is probably what you need in an RSpec hook since the Capybara::Session methods won't be mixed in there.

Note: The original question seems a bit like an XY problem. I've never had to manually close Capybara sessions after tests and it makes me think there's some other configuration error or other problem getting in the way. Considering the OP is 5 years old however, this is a moot point, although I figured I'd mention it for completeness' sake.

0

Try doing - Capybara.reset_sessions!

http://www.rubydoc.info/github/jnicklas/capybara/Capybara.reset_sessions!

Sanchita
  • 84
  • 3
  • Thanks for this Sanchita. I have already Implemented this for resetting sessions. My Question was How Driver got killed after test execution completes? Is that taken care by Capybara? If not Then we can kill the driver explicitly. – SaeeK Feb 01 '16 at 15:10