Selenium_on_rails Quick Start

Posted by on October 9th, 2007.

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 struggle 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 whole premise of testing is verifying the response to an action. 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 in the format you wish (such as HTML). Now you should be able to run this test via rake test:all. That's it!

Share:

3 Responses to “Selenium_on_rails Quick Start”

  1. Bala

    Great post Jeremy. How do you set up the test database? Do you need fixtures? Do we load data from fixtures into the test database?
  2. Jeremy

    Thanks, Bala. The project I'm on came with a bunch of fixtures, so I don't know the answer to your question first hand. However, I've seen nothing in the fixtures that looks out of the ordinary, leading me to believe that you can set up your test fixtures normally. That's why you run the server in test mode when recording browser activity - you're mimicing the circumstances of a real test run. Usually to set up the DB on a new project it's a matter of rake db:test:clone or something like that.
  3. Matt Scilipoti

    Fixtures are usually loaded in the setup of the selenium tests. Each Selenium test format has a slightly different way of doing setup. Selenese, .sel: |open|/selenium/setup| This runs the setup page. RSelenese, .rsel setup :fixtures => :all From http://wiki.rubyonrails.org/rails/pages/SeleniumIntegration: Setting up test data It’s usually not feasible to set up test data using the web GUi for each test. It could be a lengthy involved procedure that complicates the test itself, or it could even be that some data is just not possible to enter using the web GUI. In that case we can simply call the action of a special test data controller using the “open” command. (read the page for more details) Resources: http://www.openqa.org/selenium-on-rails/ http://andthennothing.net/archives/2006/02/19/new-version-of-selenium-on-rails http://wiki.rubyonrails.org/rails/pages/SeleniumIntegration

Sorry, comments are closed for this article.