I've never actually tried using selenium-webdriver
in a standalone script like that, but I have run into the same problem using selenium-webdriver
within the context of capybara
/cucumber
.
Looking at the source code for capybara
, I found this hook which explicitly closes the browser after your script is finished. If you're not using selenium-webdriver
with capybara
, then this might not be helpful, but it was helpful for me...
gems/capybara-1.1.1/lib/capybara/selenium/driver.rb
registers an at_exit
hook, which then calls quit
on the browser object:
require 'selenium-webdriver'
class Capybara::Selenium::Driver < Capybara::Driver::Base
...
def browser
unless @browser
@browser = Selenium::WebDriver.for(options[:browser], options.reject { |key,val| SPECIAL_OPTIONS.include?(key) })
main = Process.pid
at_exit do
# Store the exit status of the test run since it goes away after calling the at_exit proc...
@exit_status = $!.status if $!.is_a?(SystemExit)
quit if Process.pid == main
exit @exit_status if @exit_status # Force exit with stored status
end
end
@browser
end
You should be able to monkey-patch the quit
method so that it does nothing, like so:
Selenium::WebDriver::Driver.class_eval do
def quit
#STDOUT.puts "#{self.class}#quit: no-op"
end
end
Note: If you are using Selenium::WebDriver.for :chrome
and chromedriver
-- which you aren't, but other people might be -- I noticed that it also kills the chromedriver
process, and as soon as that "service" process is killed, the Chrome browser process that was connected to it also quits.
So I had to also prevent that service process from stopping, like so:
Selenium::WebDriver::Chrome::Service.class_eval do
def stop
#STDOUT.puts "#{self.class}#stop: no-op"
end
end
There was one other problem I ran into, which probably won't affect you, unless you're using this driver with cucumber... Even after I got it to leave the browser open, it would be left open on the "about:blank" page. It looks like this is triggered by this hook:
gems/capybara-1.1.1/lib/capybara/cucumber.rb:
After do
Capybara.reset_sessions!
end
Which calls gems/capybara-1.1.1/lib/capybara/session.rb:70:in `reset!'"
Which calls gems/capybara-1.1.1/lib/capybara/selenium/driver.rb:80:in `reset!'":
def reset!
...
@browser.navigate.to('about:blank')
...
end
And I solved that with another monkey-patch:
Capybara::Selenium::Driver.class_eval do
def reset!
end
end