Reputation: 237
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
Upvotes: 0
Views: 4110
Reputation: 798
After trying this answer I got unsatisfactory results. I believe this is because quitting the session is defined as => Disconnect from the current driver. So the driver will still be there and will eventually be handled by the configuration of Capybara and your Driver.
I got improved results with: Capybara.current_session.driver.quit.
I am using Selenium so it calls the Driver's quit method, Which has the following source:
def quit
@browser&.quit
rescue Selenium::WebDriver::Error::SessionNotCreatedError, Errno::ECONNREFUSED,
Selenium::WebDriver::Error::InvalidSessionIdError
# Browser must have already gone
rescue Selenium::WebDriver::Error::UnknownError => e
unless silenced_unknown_error_message?(e.message) # Most likely already gone
# probably already gone but not sure - so warn
warn "Ignoring Selenium UnknownError during driver quit: #{e.message}"
end
ensure
@browser = nil
end
But still was having issues. which lead me to the XY problem comment from the answer that I tried and so far have landed on a configuration change that seems to have helped a lot.
I am using Selenium Grid from a docker image and found this Answer => which prompted the driver.quit
approach but later lead to wanting to update the -cleanUpCycle
parameter
This lead me dig through the docs and find an ENV variable SE_NODE_SESSION_TIMEOUT
that handles the same functionality of -cleanUpCycle
. setting a low SE_NODE_SESSION_TIMEOUT
in tandem with increasing SE_NODE_MAX_SESSIONS
to at least 2, I could then have my stale session with availability to start up the next session while the short session timeout cleans up the stale session before I can make a 3rd request for another session.
This is obviously not an answer for everyone who is having problems with your sessions but while you may be using a different driver, the concept could be worth trying with your driver's configuration.
Upvotes: 0
Reputation: 21
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:
Capybara::DSL
directly (e.g. in a plain ruby script like I am), current_scope.session.quit
does the trick.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.
Upvotes: 2
Reputation: 49890
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).
Upvotes: 2