Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to leave browser opened even after selenium ruby script finishes

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 

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

like image 202
Anindya Sharma Avatar asked Sep 26 '11 12:09

Anindya Sharma


People also ask

How to close newly opened window in selenium?

driver. close(); is applicable for new window.

How does selenium handle pop up Ruby?

You have to use the switch_to method to deal with the pop-up. Look at the documentation of JavaScript dialogs : You can use webdriver to handle Javascript alert(), prompt() and confirm() dialogs. The API for all three is the same.


1 Answers

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
like image 74
Tyler Rick Avatar answered Nov 15 '22 14:11

Tyler Rick