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!

IE Developer Toolbar

Firefox and FireBug make a great combination. It makes working with Watir nice and easy. Unfortunately, some apps refuse to work with FireFox (IBM Maximo and Oracle SPL Customer Care and Billing, aka: “CC&B” are examples), so MSIE is the only option. Fortunately, there is a toolbar that you can install to get a watered-down Firebug equivalent; it’s called the “Microsoft Internet Explorer Developer Toolbar”. Get it here.

More WATIR news…

It looks like WATIR is becoming more and more tempting to use as a full-blown test automation tool… The one thing that was keeping me away was that it only works with Microsoft Internet Explorer. No more! I’ve discovered that it now works with FireFox and Safari on the Mac. Cross-platform regression testing of webapps using the same automated test tool is now posible. Absolute Genius.

Once I get through my shiny new “Agile Web Development with Rails”, I’ll be looking at WATIR in earnest.