Getting Started with Webrat, Selenium, Rails and Firefox 3

While working on a recent project it became clear there was a need to be able to test front-end functionality. The core javascript code was already unit tested via Blue-Ridge and Screw.Unit, but I also wanted a higher level integration test of how the javascript and back-end interacted. The are a small number of options for performing such integration tests in the browser. I’d heard a number of good reports about Webrat in conjunction with Selenium from some colleagues, so I decided to give it a try.

As I proceeded I ran into a number of issues in getting my Rails/Selenium testing environment set up. I hope to shed some light on the difficulties I encountered and ease you through the processes, making it as smooth as possible.

The first thing that’s needed is to require the necessary gems in the environment file (I prefer to use my config/environments/test.rb but others may prefer environment.rb) with these lines:

config.gem 'selenium-client', :lib => 'selenium/client' config.gem 'webrat' 

The first problem I ran into was getting Selenium to open Firefox 3.0.x correctly. I kept getting the following error:

Failed to start new browser session, shutdown browser and clear all session data org.openqa.selenium.server.RemoteCommandException: timed out waiting for window 'null' to appear 

I heard from a number of people and read in a number of places to try updating the copy of Selenium RC server in Webrat to the latest version (available at http://seleniumhq.org/download/). To do this, just replace the “selenium-server.jar” file found in the Webrat gem (in the “vendor” directory), with the one downloaded (after unarchiving the download it should be in the “selenium-remote-control-1.0.1/selenium-server-1.0.1” directory).

I found that this still did not solve the problem. It appears that the current versions of Webrat (0.4.4) and selenium-client (1.2.16) are not compatible. The easiest solution to this is to change a couple of lines in the webrat gem. This is done easiest if you have your gems unpacked into “vendor/gems”. You can find a patch with the relevant changes here: http://gist.github.com/141590. Just apply that patch to the “lib/webrat/selenium/selenium_rc_server.rb” file in the Webrat gem and Selenium should then start up just fine.

Another problem I ran into was being able to access and interact with records that I would create in a setup method. This is in part because by default Webrat sets the environment for the server sending pages to Selenium to “selenium”. Unfortunately the test script is being run in the “test” environment meaning the data you create in the script is not available in the browser.
There are two parts to this solution, the first is to make sure your Webrat configuration of the “application_environment” value is set to “:test”:

Webrat.configure do |config| config.mode = :selenium config.application_environment = :test config.application_framework = :rails end 

The second step is to ensure “transactional_fixtures” are off:

class ActiveSupport::TestCase self.use_transactional_fixtures = false end 

With all that in place you should be ready to write some Selenium based integration tests.

One last note regarding writing the tests: when using Rails route helpers, I recommend using only use the “_path” versions in your tests and avoiding the “_url” versions. Inside the tests, the host is assumed to be “www.example.com,” but on the server it will be something along the lines of “localhost:3001,” which will cause discrepancies in your tests.

Brian is a developer in our Boulder, CO, office. He loves making code readable and maintainable for clients such as Time Life and Shure.

More posts by Brian