watir: Browser.close doesn't always work

I am using chromedriver on Mac and the following gem specs: watir-webdriver (0.5.4) selenium-webdriver (>= 2.18.0)

Here is the problem. I instantiate a new browser (Watir::Browser.new :chrome), go to a url (www.microsoft.com) and do a few element search operations. Then, I call browser.close

Sometimes it works, sometimes it hangs (and unfreezes as soon as I close the browser window myself), and other times it closes and and this gets thrown:

Errno::ECONNREFUSED: Connection refused - connect(2) from /Users/mycomp/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/net/http.rb:762:in initialize' from /Users/mycomp/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/net/http.rb:762:inopen’ from /Users/mycomp/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/net/http.rb:762:in block in connect' from /Users/mycomp/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/timeout.rb:54:intimeout’ from /Users/mycomp/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/timeout.rb:99:in timeout' from /Users/mycomp/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/net/http.rb:762:inconnect’ from /Users/mycomp/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/net/http.rb:755:in do_start' from /Users/mycomp/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/net/http.rb:744:instart’ from /Users/mycomp/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/net/http.rb:1284:in request' from /Users/mycomp/.rvm/gems/ruby-1.9.3-p125/gems/rest-client-1.6.7/lib/restclient/net_http_ext.rb:51:inrequest’ from /Users/mycomp/.rvm/gems/ruby-1.9.3-p125/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/remote/http/default.rb:81:in response_for' from /Users/mycomp/.rvm/gems/ruby-1.9.3-p125/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/remote/http/default.rb:43:inrequest’ from /Users/mycomp/.rvm/gems/ruby-1.9.3-p125/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/remote/http/common.rb:40:in call' from /Users/mycomp/.rvm/gems/ruby-1.9.3-p125/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/remote/bridge.rb:598:inraw_execute’ from /Users/mycomp/.rvm/gems/ruby-1.9.3-p125/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/remote/bridge.rb:576:in execute' from /Users/mycomp/.rvm/gems/ruby-1.9.3-p125/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/remote/bridge.rb:155:ingetCurrentUrl’ from /Users/mycomp/.rvm/gems/ruby-1.9.3-p125/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/common/driver.rb:118:in current_url' from /Users/mycomp/.rvm/gems/ruby-1.9.3-p125/gems/watir-webdriver-0.5.4/lib/watir-webdriver/browser.rb:76:inurl’ from /Users/mycomp/.rvm/gems/ruby-1.9.3-p125/gems/watir-webdriver-0.5.4/lib/watir-webdriver/browser.rb:48:in inspect' from /Users/mycomp/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/commands/console.rb:47:instart’ from /Users/mycomp/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/commands/console.rb:8:in start' from /Users/mycomp/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/commands.rb:41:in<top (required)>’ from script/rails:6:in `require’

Am I missing some kind of a “wait” call or is the .close() method simply unreliable? I would like to be able to close the browser at any point, whether the page is loading or not, whether a script is running or not. As soon as I call close - just kill everything and release the resources.

About this issue

  • Original URL
  • State: closed
  • Created 12 years ago
  • Comments: 19 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Just FYI if anybody hits such issue. I had same problem. For me it was though a misconception that I have headless per browser instance instead of globally (i.e. multiple calls to Headless.new.start do not start multiple Xvfb but only one). So having multiple running browsers I did like:

browser1.close
headless1.close
browser2.close
headless2.close

First call to headless1.close makes the only Xvfb terminate and bring down any browsers with it. So second call to browser.close resulted in the error because browser is already gone.

For what it’s worth, here’s what I ended up doing (still using Chrome):

      # First, try to quit the browser the normal way
      begin
        Timeout::timeout(2) { @browser.close }
      rescue Timeout::Error
        # Well, that didn't work. Bring in the artillery!
        browser_pid = @browser.driver.instance_variable_get(:@bridge).instance_variable_get(:@service).instance_variable_get(:@process).pid
        begin
          ::Process.kill('KILL', browser_pid)
        ensure
          @browser.close
        end
      end