I am running selenium tests with firefox and chrome drivers. I get a connection refused error when I run the tests in as a block

Test example :

require 'minitest_helper'

headless = Headless.new
browser = Selenium::WebDriver.for :chrome

describe "Test Example" do

  after :all do
  it "test" do
    browser.navigate.to "http://google.com"

stack trace:

1) Error:
Example test#test_0001_test:
Errno::ECONNREFUSED: Connection refused - connect(2)
    /usr/local/rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/net/http.rb:762:in `initialize'
    /usr/local/rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/net/http.rb:762:in `open'
    /usr/local/rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/net/http.rb:762:in `block in connect'
    /usr/local/rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/timeout.rb:54:in `timeout'
    /usr/local/rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/timeout.rb:99:in `timeout'
    /usr/local/rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/net/http.rb:762:in `connect'
    /usr/local/rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/net/http.rb:755:in `do_start'
    /usr/local/rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/net/http.rb:744:in `start'
    /usr/local/rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/net/http.rb:1284:in `request'
    /usr/local/rvm/gems/ruby-1.9.3-p125@cam_integration/gems/selenium-webdriver-2.31.0/lib/selenium/webdriver/remote/http/default.rb:83:in `response_for'
    /usr/local/rvm/gems/ruby-1.9.3-p125@cam_integration/gems/selenium-webdriver-2.31.0/lib/selenium/webdriver/remote/http/default.rb:39:in `request'
    /usr/local/rvm/gems/ruby-1.9.3-p125@cam_integration/gems/selenium-webdriver-2.31.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
    /usr/local/rvm/gems/ruby-1.9.3-p125@cam_integration/gems/selenium-webdriver-2.31.0/lib/selenium/webdriver/remote/bridge.rb:616:in `raw_execute'
    /usr/local/rvm/gems/ruby-1.9.3-p125@cam_integration/gems/selenium-webdriver-2.31.0/lib/selenium/webdriver/remote/bridge.rb:594:in `execute'
    /usr/local/rvm/gems/ruby-1.9.3-p125@cam_integration/gems/selenium-webdriver-2.31.0/lib/selenium/webdriver/remote/bridge.rb:100:in `get'
    /usr/local/rvm/gems/ruby-1.9.3-p125@cam_integration/gems/selenium-webdriver-2.31.0/lib/selenium/webdriver/common/navigation.rb:14:in `to'
    test/integration/signup_test.rb:13:in `block (2 levels) in <main>'

1 tests, 0 assertions, 0 failures, 1 errors, 0 skips

2 Answers 2


I was facing a similar issue, and found a solution in this article: http://swdandruby.wordpress.com/2013/05/11/headless-gem-causes-errnoeconnrefused/

You have to specify a different display number for each browser headless instances you need.

So if you want to run at the same time chrome and firefox in separate headless mode, you can do something like:

browsers = {}

[:chrome, :firefox].each do |browser|
  Headless.new(display: rand(99)).start
  browsers[browser] = Selenium::WebDriver.for browser

browsers[:chrome].navigate.to "http://google.com"
browsers[:firefox].navigate.to "http://mozilla.org"

This will spawn two separate headless process. Note that you shouldn't rely on random things and better find some kind of unique ids.

  • I wrote the blog post reference above. The way I go about this on my CI environment is just assign the CI build job # to the display using the environmental variable set by the job. Jobs are usually on different number iterations, so this seems to work well. The problems with using a random number are the off chance that the same random numbers could be chosen for multiple instances, as well as the difficulty in knowing which display was used incase you want to VNC in to the headless display and observe the interactions.
    – bbbco
    Aug 27, 2013 at 14:33

Another way to fix this problem is to set reuse to false when you call Headless.new and let it automatically pick another display number if the current one is in use:

Headless.new(reuse: false)

For more information, see the source code to Headless at https://github.com/leonid-shevtsov/headless/blob/master/lib/headless.rb#L64, which says this:

reuse (default true) - if given display server already exists, should we use it or try another?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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