One of the things I like about Rails is the built-in support for testing – it’s almost hard not to write tests. But even if I write extensive tests (currently “Code to Test Ratio: 1:3.4” according to
rake stats in a small toy project) I still feel that it’s not enough. I occasionally get the urge to fire up the browser and click around to test that everything works. You shouldn’t have to “click around” to test that you haven’t broken anything that often (although it’s probably good to use the browser for checking usability).
However, I didn’t think that vanilla Selenium worked as smooth as it should in a Rails context. So I’ve been writing a plug-in that makes the experience much better, IMHO. It took a bit longer than what I thought, but I’ve been busy and stuff like this takes time to get into a good enough state to release to the world.
Please try it out and let me know if you have any problems using it or if you have suggestions for improvements!
Selenium on Rails provides an easy way to test Rails application with Selenium.This plugin does four things:
- No need to create suite files, they are generated on the fly—one suite per directory in /test/selenium (suites can be nested).
- Instead of writing the test cases in HTML you can use ERb/RHTML or Selenese (a template provided by this plugin).
- Loading of fixtures and wiping of session (/selenium/setup).
Selenium on Rails is disabled for production environments.
- Selenium needs to be available. It could either be installed as a gem (gem install selenium) or in /vendor/selenium/.
- Install Selenium on Rails: script/plugin install http://andthennothing.net/svn/public/selenium_on_rails/
- If RedCloth is available the Selenese test cases can use it for better markup.
- Run the Rakefile in the plugin’s directory to run the tests in order to see that everything works. (If RedCloth isn’t installed a few tests will fail since they assume RedCloth is installed.)
- Create a test case: script/generate selenium login
- Start the server: script/server
- Point your browser to http://localhost:3000/selenium
- If everything works as expected you should see the Selenium test runner. The north east frame contains all your test cases (just one for now), and the north frame contains your test case.
Selenium has support for user_extension.js which is a way to extend the functionality of Selenium. However there is currently no easy way to add such a file in Selenium on Rails.
It would be nice if common parts of test cases could be stored in partials.
More setup/teardown support?
Currently there is only support to load fixtures and to wipe the session in /selenium/setup. Is there a need for more kinds of setups or teardowns?
- Jon Tirsen—initial inspiration
Jonas Bengtsson, firstname.lastname@example.org, http://andthennothing.net
The first defect fix is out. It should now work on EdgeRails as well as Rails 1.0.0 (might work on other versions). And the tests ought to pass on non-Windows machines. Thanks Wayne for reporting these errors!
script/plugin update (or reinstall the plugin if that doesn’t work)
Another defect fix to make sure .files, and files~ are handled correctly, and updated the tests (they work on Windows and NetBSD at least). Thanks Wayne, Vincent and Shinya!