Selenium_on_rails Quick Start

Over the past few days I've been diving into Selenium, an automated functional testing tool for web apps. It's pretty slick - in fact, like most of the technologies I've been using over the past two years, it does so much for you that the obstacle always ends up being a matter of getting out of its way and letting it do its thing. So I'm gonna throw out some pointers on using Selenium within the Rails testing framework via the selenium_on_rails plugin; the only caveat I'll mention is that this post is especially geared towards the newbie wading into a project with pre-existing selenium tests. So I'm focusing on how you can get started with writing tests and integrating them into what's already there.

Open up firefox and install the Selenium IDE extension. This allows you to record your test by simply using the browser. The IDE keeps track of where you click - just make sure the record button is on. Go ahead and click through your test application and watch as the IDE records your actions.

Now you need to go into your selenium_on_rails plugin area and edit config.yml. In the browser config area, make sure it's pointing to your browser binary. For example, I kept most of the defaults, but made sure the following lines were there, uncommented:environments:   - test browsers:   firefox: '/Applications/Firefox.app/Contents/MacOS/firefox-bin'

Next, especially if this is a project with existing tests, make sure you've installed the right user extensions. These are prefabbed actions for doing things like logging in via javascript. In my project I found this file in

vendor/plugins/selenium_on_rails/selenium_core/scripts/user_extensions.js

All you need to do is point the IDE at this file by opening the IDE window and navigating in the top menu to "Options > Options > General tab". Put the path to user_extensions.js in the input box entitled "Selenium Core extensions (user-extensions.js)".

OK, so let's take a look at the existing selenium tests. They're probably in test/selenium. If there's subdirectories under that, these are probably test "suites". For example, in the project I'm working on, they're organized depending on which type of user the test is geared towards: user or admin. The actual test scripts can be recorded in a variety of formats; mine are all HTML tables, where each row is a step in the process.

So let's get to recording tests. First, start your server in the test environment:

mongrel_rails start -e test

The key to testing is executing an action and verifying the response. In Selenium, this is accomplished via assertion statements. The IDE makes this elementary: when you've reached a page on which you need to verify the output, just highlight the text and right click. At the bottom of the popup menu, several approaches to verifying the text are mentioned. I don't understand all the "selenese" yet but there's simple commands like "assertText" that ensure a particular passage is included on the page - that should get you started.

So you've recorded the steps necessary - now click "File > Export Test As" and save it in the correct test directory. Now you should be able to run this test via rake test:all. That's it!

Read this article
Written on Monday, October 08, 2007