Wednesday, 1 July 2009

Selenium Web Testing

Yep, I’ll admit it - it’s been quite a while since my last post. In the two and a bit months that have passed, I’ve been quite busy in both a work and personal capacity. I’ve been toying with a few development bits and pieces that I’ll (hopefully) get to posting about in the not-too-distant future.

For today though, the topic is web application testing with Selenium. There are a bunch of web testing frameworks available, but the ones I’ve heard most about are Selenium (free), WATIN (free), and Visual Studio’s built-in web testing framework within the VSTS Test edition.

Selenium appealed most for the first cab off the rank purely based on its ease of use and browser support. Each of the others will no doubt have their benefits and supporters, but this is totally a personal choice. Read on if you wish :)

Getting the Bits

Getting started with Selenium is a snap, with a simple download or two, you can be up and running in minutes.

Those two alone will be enough to get you going. You’ll be able to script up basic test scenarios, save to file, and re-run your tests as often as you like. To make better use of Selenium (and the rest of this post), you’ll also need to:

  • Get Selenium RC. This link goes to v1.0.1 (current release at the time of posting, check for the latest).
  • Get the FireBug Firefox plugin. As with the IE8 developer tools, this tools invaluable to web devs, and it assists greatly when building out your Selenium tests.

Have a look at the Selenium “Getting Started” links at http://seleniumhq.org/ for info on what to do once you have completed the downloads and installs. Suffice to say, it’s a snap.

Test Export Templates

With your first test scenario(s) out of the way, it’s time to get into integrating them into your unit and/or build verification tests in TFS or CruiseControl.NET.

Out of the box, Selenium IDE supports Java, Groovy, C# (NUnit), Perl, PHP, Python, and Ruby. With a few minor modifications to the C# NUnit template, VS Test support is not too far off.

  • In Selenium IDE, go to Options > Options... > Format
  • Click Add, set the name to “C# (VSTest)”, and paste in your code template content. My code template is linked at the bottom of this post.
  • Click OK to close out the template and options windows, and you’re all set.
  • Export your test scenario to a Visual Studio test class by going to File > Export Test Case As... > C# (VSTest).

Before Running Your Test...

You will need to start up the Selenium RC server. To do this:

  • Download the latest version of the Java SDK or Runtime
  • Set the your JAVA_HOME environment variable to your Java install path
  • Add %JAVA_HOME%\bin to your system path
  • Open a command prompt and change directory to the Selenium Server directory located in your unzipped Selenium RC directory.
  • Start the server by running java –jar selenium-server.jar. Default settings should be fine, unless you have something already running on port 4444.

Running Your Tests

Running your Selenium test scripts is as easy as adding a reference to the Selenium RC .NET driver assembly under your Selenium RC distribution folder and CTRL+R CTRL+T. You’ll see a browser window open which acts as a host for running your script, one containing your web application’s progress, and you’ll also see your test’s progress in the Selenium Server console.

At the end of your test run, you should have a nice green tick.

Download my Selenium export template for C# Visual Studio test projects from here.

No comments:

Post a Comment