17

I am using a ruby script with selenium web-driver, for automating an web page login. The issue is after script finishes it closes the browser also. I want to keep the browser opened even after the script finishes. Is there any way by which I can keep browser open after the test do something else with the browser window?

I am doing like this.

if browser == "Firefox"
     driver = Selenium::WebDriver.for :firefox
end

if stack == "example.com"
      driver.get "http://www.example.com/tests/
end

element = driver.find_element :name => "email"
element.clear
element.send_keys username

element = driver.find_element :name => "password"
element.clear
element.send_keys password

element = driver.find_element :name => "commit"
element.submit 

===================================================

8 Answers 8

13

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
13

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.

1
  • I presume this would block a single threaded server being debugged from serving other requests from a browser :(
    – mlt
    Commented Dec 3, 2020 at 21:54
12

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
  end
end

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!

4

If you run the test with debugging enabled and drop a debugger line at the end it should leave the browser open. Look at the ruby-debug gem. It also might be worth checking out this Railscast about Pry.

1
  • 3
    That would definitely be the easiest solution, if you're okay with pausing the execution of your script. If you wanted your script to finish/quit and still leave the browser open, this solution wouldn't work.
    – Tyler Rick
    Commented Sep 27, 2011 at 18:30
2

Here some snippet in Python:

Putting this at the end of your script, will leave the browser windows open and ends the script when it is closed manually (requires some imports at the beginning):

import time
import sys
...
while 1:
    time.sleep(5)
    try:
       b = browser.find_by_tag("body")      
    except:
      sys.exit()
2
  • Note for users of more recent versions of Selenium: I found that I had to change find_by_tag to find_element_by_tag_name.
    – kuzzooroo
    Commented Feb 25, 2015 at 3:41
  • this just opens the browser, insta closes it, then opens it again after minutes in python Splinter (which uses selenium which uses chromedriver) but behaviour can differ.
    – Michahell
    Commented Mar 27, 2015 at 2:06
2

Watir's until function will hold the script until a condition returns true. Set the timeout to infinity and check for all the windows to be closed.

Watir::Wait.until(timeout: Float::INFINITY) {@browser.windows.length == 0}
@browser.close

Put this at the end of your script and close all the tabs manually to exit.

1
  • Edited to explain.
    – nikojpapa
    Commented Aug 6, 2018 at 16:09
1

This is largely what the other commenters have said already about overriding quit on the driver, but here's the simplified code I ended up using for a standalone Capybara script:

require 'capybara'

session = Capybara::Session.new(:selenium)
session.driver.class.class_eval { def quit; end }
session.visit('http://localhost:3000')
# ... other stuff

Assuming you have a web server running on localhost:3000, this will launch Firefox, navigate to this URL and leave the browser window open.

0

What test framework are you using?

Usually your test framework has some sort of setup and teardown methods that you can define which allows you to start the browser

@driver = Selenium::WebDriver.for :firefox

in the setup method, and destroy the browser

@driver.quit

in teardown.

Just put some logic around the teardown portion of your scripts to only destroy the browser when you want to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.