Subscribe Favorite

Selenium on Rails

Written on December 9, 2007 by Eka Riyanti

Selenium Core provides an easy way to test Rails application.

This plugin does four things:

  1. The Selenium Core files don‘t have to pollute /public.
  2. No need to create suite files, they are generated on the fly — one suite per directory in /test/selenium (suites can be nested).
  3. Instead of writing the test cases in HTML you can use a number of better formats (see Formats).
  4. Loading of fixtures and wiping of session (/selenium/setup).

Installation

  1. Install Selenium on Rails: script/plugin install http://svn.openqa.org/svn/selenium-on-rails/selenium-on-rails
  2. If you‘re on Windows, gem install win32-open3
  3. If the RedCloth gem is available the Selenese test cases can use it for better markup.
  4. 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.)
  5. Create a test case: script/generate selenium login
  6. Start the server: script/server -e test
  7. Point your browser to localhost:3000/selenium
  8. 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.

Formats

The test cases can be written in a number of formats. which one you choose is a matter of taste. You can generate your test files by running sript/generate selenium or by creating them manually in your /test/selenium directory.

  • Selenese, .Sel

Selenese is the dumbest format (in a good way). You just write your commands delimited by | characters.

 |open|/selenium/setup|
 |open|/|
 |goBack|

If you don‘t want to write Selenese tests by hand you can use SeleniumIDE which has support for Selenese.

SeleniumIDE makes it super easy to record test and edit them.

  • RSelenese, .rSel

RSelenese enable you to write your tests in Ruby.

 setup :fixtures => :all
 open '/'
 assert_title 'Home'
 ('a'..'z').each {|c| open :controller => 'user', :action => 'create', :name => c }

See SeleniumOnRails::TestBuilder for available commands.

Configuration

There are a number of settings available. You make them by renaming config.yml.example to config.yml and make your changes in that file.

  • Environment

Per default this plugin is only available in test environment. You can change this by setting environments, such as:

 #config.yml
 environments:
   - test
   - development
  • Selenium Core Path

If you don‘t want to use the bundled Selenium Core version you can set selenium_path to the directory where Selenium Core is stored.

 #config.yml
 selenium_path: 'c:selenium'

Test Acceptance

You can run all your Selenium tests as a Rake task. First, if you‘re on Windows, you have to make sure win32-open3 is installed. Then you have to configure which browsers you want to run, like this:

#config.yml browsers: firefox: ‘c:\Program Files\Mozilla Firefox\firefox.exe’ ie: ‘c:\Program Files\Internet Explorer\iexplore.exe’ Now you‘re all set. First start a server:

script/server -e test Then run the tests:

rake test:acceptance Now it should work, otherwise let me know!

Store results

If you want to store the results from a test:acceptance you just need to set in which directory they should be stored:

 #config.yml
 result_dir: 'c:result'

So when you run rake test:acceptance the tables with the results will be stored as .html files in that directory.

This can be useful especially for continous integration.

Read more on: OpenQA project page.

If you enjoyed this post Subscribe to our feed

6 Comments on “Selenium on Rails”

  1. Custom Rails » Selenium on <b>Rails</b> |

    […] Check This Out! While looking through the blogosphere we stumbled on an interesting post today. Here’s a quick excerpt: Install Selenium on Rails: script/plugin install http://svn.openqa.org/svn/selenium-on-rails/selenium-on-rails; If you‘re on Windows, gem install win32-open3; If the RedCloth gem is available the Selenese test cases can use it for … […]

  2. dan » Selenium on Rails |

    […] Check it out! While looking through the blogosphere we stumbled on an interesting post today.Here’s a quick excerptThe test cases can be written in a number of formats. which one you choose is a matter of taste. You can generate your test files by running sript/generate selenium or by creating them manually in your /test/selenium directory. … […]

  3. » The Links » roarin’ reporter |

    […] Selenium on Rails […]

  4. Harry |
  5. Harry |

    Does anyone know how to do a multiple session login test in selenium on rails….

  6. Harry |

    anybody have an idea on mutisession login test

Leave a Reply