So you'd like to run Cucumber and Selenium for your Rails integration tests - with everything vendored - would you?

You may have one day run into the following problem if you are a big Rails BDD developer: You’re trying to setup Cucumber to run integration tests with Webrat and Selenium, and you have all of your web application’s dependencies vendored. There are numerous reasons for doing this, and perhaps most poignantly, it’s much easier to make sure everything is on the same page that way.
However, for some reason, you can’t get the Selenium tests to run without installing mongrel_rails at a system level. If you try to run tests, you’ll probably get a notification somewhere along the following lines:
==> Failed to boot the Rails application server... exiting!
Verify you can start a Rails server on port 3001 with the following command:
mongrel_rails start -d --chdir='/your/path/to/your/app' --port=3001 --environment=cucumber --pid /your/path/to/your/app/tmp/pids/mongrel_selenium.pid &r
The first time you saw this, you may have exclaimed “The hell? Of course I can start an instance of mongrel!” (and then you type script/server start..) I’ve managed to work through this, and get every integration test to run smoothly (and pass) while being completely localized. Let me share with you how this is done – it’s pretty simple.
The reason behind this irksome behavior has very little to do with Selenium, or selenium-client. It really has everything to do with the Webrat gem and the way in which it invokes a server instance to be used by Selenium. In specific, it just evokes “mongrel_rails” on the command line – literally just echoing it out to the shell. (You may have wondered why mongrel is not listed as a dependency for Webrat, but running Cucumber tests without it causes a no such file to load - mongrel error.)
This works , if you have mongrel_rails installed system wide (on a UNIX-based system, there will be a executable in your bin directory and you should be golden after that), but if you have it vendored, or would like to use something other than mongrel to act as your web server “black box”, then you will have to do a little modification to Webrat.
I’m going to try to get this change into the webrat gem – I feel it makes sense. Until then, here is your solution. Create a new file in lib called webrat_extensions.rb, and enter in the following.
Save it. Webrat will now run Rails’s script/server command in a daemon in order run integration tests with Selenium. You could even get a little fancy with it, and include a “server_type” variable if you wanted to specify between, say, mongrel and Webrick.
After saving, open up your selenium.rb (or whatever you have named it) file in features/support. You will have to require the classes that you are modifying here, or else they will be overwritten, so enter in the following:

require 'webrat/selenium'
require 'webrat/selenium/selenium_session'
require 'webrat/selenium/application_servers/rails'

require File.expand_path(File.dirname(__FILE__) + '/../../lib/webrat_extensions')

This should set you up pretty nicely. You should now be able to run rake cucumber and run your selenium tests without mongrel_rails or any other gem installed system-wide. Let me know if I’ve missed any of the details.

0 comments