Selenium-on-Rails 2.0

Selenium-on-Rails is a fast, partial alternative to creating full blown tests in Rails. It doesn’t do everything but it’s better than nothing and is super simple to use, requires no coding and does a pretty good job of “run through the pages” automation testing before deploys.

Setting up in your project

  1. Install the plugin:
    script/plugin install
  2. Rails 2.0 doesn’t allow “-” in plugin names:
    mv vendor/plugins/selenium-on-rails vendor/plugins/selenium_on_rails
  3. I couldn’t get selenium’s routes.rb to register so I did this hack: copy this to the top of your config/routes.rb:
    map.connect 'selenium/setup',
      :controller => 'selenium', :action => 'setup'
    map.connect 'selenium/tests/*testname',
      :controller => 'selenium', :action => 'test_file'
    map.connect 'selenium/postResults',
      :controller => 'selenium', :action => 'record'
    map.connect 'selenium/postResults/:logFile',
      :controller => 'selenium', :action => 'record', :requirements => { :logFile => /.*/ }
    map.connect 'selenium/*filename',
      :controller => 'selenium', :action => 'support_file'
  4. Create a vendor/plugins/selenium_on_rails/config.yml – I use this (see config.yml.example for more info):
      - test
      - development
      # Mac OS X
      firefox: '/Applications/'
  5. Grab their Firefox IDE plugin to help auto generate tests:

Using the test IDE

Activate the Selenium IDE in Firefox:

Selenium IDE

You now have a window where you can record tests.

Selenium IDE

This is super simple to use.

  1. Set the base url to your project (likely http://localhost:3000)
  2. In the main browser (not the ide) go to the same url (http://localhost:3000)
  3. Hit the record button (red dot on the top right of the IDE)
  4. In the main browser do what you want tested (click around, etc.).
  5. When you’re done – click the same red dot to stop recording
  6. You can now go and right click on the list of commands recorded (in the Table tab) and insert new commands which allow you to test various things: assertBodyText, etc.
  7. When you’re done switch to the Source tab and File > Export Test As… HTML to your_app/test/selenium/test_some_stuff.rhtml
  8. Now, when you go to http://localhost:3000/selenium you should see your test, just click on it and hit play.