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.