How to leave browser opened even after selenium ruby script finishes How to leave browser opened even after selenium ruby script finishes selenium selenium

How to leave browser opened even after selenium ruby script finishes


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


A quick hack that worked for me:

sleep(9000)

in your test gives you a fair amount of time to play around with the browser before the driver closes it.


This is the code that worked for me with Capybara

Capybara::Selenium::Driver.class_eval do  def quit    puts "Press RETURN to quit the browser"    $stdin.gets    @browser.quit  rescue Errno::ECONNREFUSED    # Browser must have already gone  endend

It's a monkey patching of what I found in gems/capybara-1.1.2/lib/capybara/selenium/driver.rb

I just added the puts and the gets lines. Selenium::WebDriver::Driver was giving me a not found error probably because I'm getting selenium from within capybara.

This is useful to see what's the code that generated an error but there is a downside: the browser stops past the last page and displays a blank screen. I have to click the back button to get to the page with the error which might not always work. Does anybody know why the browser loads that empty page and save me the time to dig into the capybara code? Thanks!