Search code examples
rubygoogle-chromeseleniumwatir-webdriver

chrome browser closes automatically after the program finishes in ruby using watir


I am using chrome 56, chrome driver 2.27(latest release) with selenium web driver 3.1.0. Referring to the issue(https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/1811) where chrome closes all the instances once the program finishes and it does not give me a chance to debug. I just want to know if this is fixed then why it is still happening ? or i am missing something ? I am using the following code. Any help is appreciated.

require "uri"
require "net/http"
require 'watir-webdriver'
require 'selenium-webdriver'

@b = Watir::Browser.new :chrome
@b.goto 'http://www.google.com'


Solution

  • Firstly, watir-webdriver gem is deprecated. The updated code is in the watir gem. Also, you shouldn't need to require any of those other gems directly.

    The chromedriver service is stopped when the ruby process exits. If you do not want the browsers that were started by chromedriver to close as well, you need to use the detach parameter. Currently this is done like so:

    require 'watir'
    
    caps = Selenium::WebDriver::Remote::Capabilities.chrome
    caps[:chrome_options] = {detach: true}
    @b = Watir::Browser.new :chrome, desired_capabilities: caps