Selenium/Chrome: connection refused SOMETIMES

I am trying to do browser testing using Selenium, Chrome and a Ruby/Rails application using RSpec an Capybara. My builds pass successfully about every 1 in 4 runs; in the other 3, at some point all tests using Selenium fail with “connection refused” errors:

#   Connection refused - connect(2) for "" port 9515

I don’t have any other debug information to go on other than the connection refused errors. I also cannot reproduce it locally. I have tried using different versions of Chrome, chromedriver, running Selenium locally as a .jar and using a Selenium chrome-standalone docker image. But, as the tests sometimes pass and sometimes fail, I suspect the basic configuration is alright, but something in my tests breaks it, or some memory limit is hit somewhere.

Does anyone have any idea what might be wrong, or what might possibly fix it?

At a guess, you have a race condition between your tests starting and the Selenium server being ready to accept connections. Try waiting for five seconds before your tests using sleep.

If that fixes it, you can probably use a waitforit script that repeatedly tests a specified port and quits once the port is available.

Thanks for the response. I’m going to give it a try. However, I do think the first failures only occur a couple of minutes into the test command.

I think I have found a test order in which the error does not occur, even without using the waitforit script. That means my environment is probably fine and something in my tests breaks it, although I’m at a loss to explain what. A tool like rspec --bisect will not produce any output for too long to be able to run it on CircleCI.

BTW I think the best way to wait for a service to come online is the ill-named dockerize tool.

Despite what that page says, I don’t have to manually download that tool when using CircleCI prebuilt images.

1 Like