FireWatir JSSH “Unable to connect” – FIXED!

Firewatir is awesome. But… sometimes Firefox takes too long to load (eg: if you have loads of plugins) causing firewatir to timeout and give the following error:

Unable to connect to machine : 127.0.0.1 on port 9997. Make sure that JSSh is properly installed and Firefox is running with '-jssh' option

This was a major pain; painful enough to send me looking through the firewatir code in the hope that I’ll be able to fix it. Turns out the logic that deals with starting firefox and establishing a connection to JSSH needs only a minor change to fix the problem. The relevant code is in the firefox.rb file within the firewatir gem, specifically the set_defaults method. Here’s a monkeypatch to override the necessary method:


require "rubygems"
require "firewatir"

class FireWatir::Firefox
  # fixes the timeout caused by firefox taking ages to
  # load, preventing a connection from being made to JSSH
  def set_defaults(no_of_tries = 0)
    begin
      $jssh_socket = TCPSocket::new(MACHINE_IP, "9997")
      $jssh_socket.sync = true
      read_socket()
    rescue
      no_of_tries += 1
      sleep 0.1
      retry if no_of_tries < 300
      raise UnableToStartJSShException, "Unable to connect to machine : #{MACHINE_IP} on port 9997. Make sure that JSSh is properly installed and Firefox is running with '-jssh' option"
    end
    @error_checkers = []
  end
end

To use the patch, place it in a file that is read in before you use firewatir for the first time.

Firewatir tries to connect 3 times to JSSH after waiting for 2 seconds for Firefox to load. This is usually enough. But again, there are scenarios where 2 seconds and 3 connection attempts just isn’t enough. The very simple and slightly hacky fix that I tried worked beautifully… instead of trying 3 times, try 300 times instead with a simple sleep 0.1 just before the retry. This gives Firefox around 30 seconds to get its act together. As soon as it is ready, the test begins. It hasn’t caused any execution slowdown – in fact, now that I don’t have to run ‘failed’ tests at the end of a test run, I can just run everything once, saving loads of time! The joys of one-click-testing…
Since putting in this fix, I haven’t seen the problem again!

4 thoughts on “FireWatir JSSH “Unable to connect” – FIXED!

  1. That ‘fix’ doesn’t work for me.

    FireWatir seems pretty awful to be honest. It worked for months then just stopped working. I haven’t changed anything it just decided to stop working… lame.

  2. Mike,

    The monkey patch above most certainly *is* a fix, as evidenced by the many emails I have thanking me for it.

    Firewatir may not be the finest bit of software engineering out there, but it (almost always) does the job. I suspect that there’s something wrong with your setup that’s unrelated to firewatir or the above patch.

    And anyway, why aren’t you using watir-webdriver instead of firewatir?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>