2

I'm trying to get watir running on a rackspace linux box (fedora 14) but when I try to launch watir I get this error

Selenium::WebDriver::Error::WebDriverError: unable to obtain stable firefox connection in 60 seconds

I started with firefox 3.6.24 and upgraded to firefox 10.0.2. When I upgraded I got a warning that fedora 14 is EOL for firefox and no longer supported.

I installed watir via rubygems and here are my versions.

watir-webdriver (0.5.3)
selenium-webdriver (2.19.0)

I also installed GNOME thinking that firefox and selenium need a windowing system to operate correctly. What am I missing? All I'm trying to do is automate browser actions on a remote linux server. Everything's working locally using Mac OSX Lion.


EDIT 1

Ok so this is getting pretty strange. The following script works on the server when I run it from the command line.

#! /usr/bin/env ruby

require 'watir-webdriver'
require 'headless'

@headless = Headless.new
@headless.start
browser = Watir::Browser.new :ff
browser.goto("http://www.google.com")

But when I run the code in the rails environment it doesn't work at all. I do have the headless gem installed. Things are separated into modules in my code, but I verified that the following code exhibits the exact same behavior.

In routes.rb I have a resource

resources :stories

Then in the stories_controller I have this code

def create
    @headless = Headless.new
    @headless.start
    browser = Watir::Browser.new :ff
    browser.goto("http://google.com")
    count = browser.as.count
    respond_to do |format|
      format.json {render :json => {:count => count}.to_json}
    end
end

I send a post request to server_ip/stories and get an error response. In production.log I find the following error

Selenium::WebDriver::Error::WebDriverError (unable to obtain stable firefox connection in 60 seconds (127.0.0.1:7055)):

I'm using Rails 3.0.3 on Apache/2.2.17 (Unix) connected with Phusion Passenger installed through the Passenger (3.0.11) gem.

When I run just the script (which works) from the command line, I observe that Xvfb is launched and two firefox processes are started, one of which immediately becomes defunct. The second process keeps running, which I assume is correctly executing the script.

When I observe the same code running from the rails environment, I see the same chain of events but the firefox process that's not defunct appears and then immediately stops execution. Then I see a chain of PhusionHelper processes start.

Any idea why the real firefox process would stop in rails when running through apache --> phusion --> rails but not within the ruby interpreter? I even verified the following works

rails console
h = Headless.new
h.start
b = Watir::Browser.new :ff

So I'm not convinced it's simply a rails environment problem.


EDIT 2

Tried the exact same code but in chrome using the ChromeDriver and watir-webdriver and I get the exact same results as before except within rails I get this error:

Selenium::WebDriver::Error::UnknownError (Unable to either launch or connect to Chrome. Please check that ChromeDriver is up-to-date. Using Chrome binary at: /opt/google/chrome/google-chrome):

EDIT 3

I narrowed the problem down to Phusion Passenger. From what I understand, webdriver connects to browsers through a server/client architecture over a stream socket. The connection happens in socket_poller.rb in selenium_webdriver here:

      addr     = Socket.getaddrinfo(@host, @port, Socket::AF_INET, Socket::SOCK_STREAM)
      sock     = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
      sockaddr = Socket.pack_sockaddr_in(@port, addr[0][3])

      begin
        sock.connect_nonblock sockaddr

...... AND get's rescued here

    rescue *NOT_CONNECTED_ERRORS
      sock.close if sock
      $stderr.puts [@host, @port].inspect if $DEBUG
      false
    end

Turns out phusion_passenger does not allow you to seek or rewind on sockets and provides a wrapper that does not respond to those methods. You can read more here http://rubydoc.info/github/FooBarWidget/passenger/master/PhusionPassenger/Utils/UnseekableSocket I'm not sure if this is the exact problem, but I think it must be related to this.

So that explains why running this service inside the Phusion Passenger framework fails, but works elsewhere. I was able to solve this issue by creating a simple server outside the Passenger/Rails framework and executing the script from there.

I welcome any more insights into the exact cause of this.

kikster
  • 51
  • 2
  • 4
  • This is an webdriver issue, I edited your title and retagged accordingly. Watir-webdriver uses webdriver to do all the communication with the browser (and wraps that in the far more friendly watir API) in this case the issue is that webdriver cannot connect to firefox, a problem on the webdriver side of watir-webdriver. – Chuck van der Linden Feb 19 '12 at 03:48
  • I solved my similar problem with this topic: http://stackoverflow.com/q/5782055/270433 Be careful of the user running your rails app – Sidhannowe Jul 16 '12 at 17:02

2 Answers2

1

Generally, this error occurs when Firefox can't start. It's mostly because your rackspace box is headless, while Firefox requires available display to run. Install Xvfb and try once again using headless gem - http://rubygems.org/gems/headless

p0deje
  • 3,903
  • 1
  • 26
  • 37
  • Makes sense. I hadn't installed Xvfb on the system, so I went ahead and did that. The strange thing is that everything works great when I use the rails console (h = Headless.new; h.start; Watir::Browser.new :ff), but when I execute the code from the context of a rails app it doesn't seem to work. Still says it can't obtain a stable version of firefox. – kikster Feb 20 '12 at 06:03
  • I updated the question with more details. Thanks a ton for your help – kikster Feb 20 '12 at 07:17
  • Weird. First, does it work in development mode? Second, why would you want create Watir-WebDriver instance in controller? – p0deje Feb 20 '12 at 08:21
  • Yes it works in development mode (WebBrick). I'm creating an Api and the controller invokes methods that are stored in some helper modules. Those modules do some processing and report back to the controller the success or failure of those operations. The controller then sends a log report back to the client. So the calls to Watir are not necessarily in the controller, but they are invoked within the controller's context. I'm starting to wonder if there's something unique to how Passenger treats classes that inherit from ApplicationController as opposed to how rails console treats them. – kikster Feb 20 '12 at 22:52
  • Sorry, but I still don't understand why you'd need to use Watir in development/production mode. It's supposed to be catered as Capybara: 1) In test environment; 2) With separate application instance running on separate server (e.g. Webrick). The problem may be related to separate processes (one for application with Passenger, one for Firefox), but I'm not sure. – p0deje Feb 21 '12 at 08:37
  • You're right. Perhaps for this task something like phantom js would have been better. Updated question with how I found a solution to the problem. – kikster Feb 24 '12 at 18:34
0

I run into a very similar issue. In my case, I have a simple environment, without Phusion. I'm using headless mode, even if I have graphics interface installed. The script was running properly as root, an it was crashing when running as a different user. I solved it by running manually Firefox once (as the requested user) and removing a every check for updates.

HTH

sbos61
  • 554
  • 2
  • 9