Testing Web Apps with Selenium

Inspiration can be found in the most unexpected places: about this time last year I was introduced to the Selenium suite of browser automation tools while chatting to an acquaintance at a wedding. Since then I’ve used two of the products, Selenium IDE and Selenium Remote Control (I’m looking forward to experimenting with WebDriver soon). The IDE allows you to record your behaviour on a website and I find it particularly useful for reproducing bugs where a number of steps are required to reach the problem area. The IDE is convenient, but only works in Firefox. For testing in other browsers you can use RC.

Option 1: Firefox-only testing using recording functionality

Selenium IDE is a Firefox plugin and can be downloaded from here. You’ll probably have to click ‘Allow’ on a message about ‘Firefox prevented this site from asking you to install software on your computer’, and may get other warning messages as you install it.

Using the recorder

  • When you open the tool from the Web Developer menu, the record button is automatically ‘On’.
  • Navigating through your site as if you were using it will record your tests for you.
  • Right-clicking on elements brings up a context menu for you to choose commands from.
  • Tests are saved in HTML table format and can be opened up and edited by hand if desired.

Running Tests

  1. Record and save your tests
  2. If you’re testing a site on localhost, make sure your site is running
  3. Run your tests from the Selenium IDE window. (NB it will overwrite the active window in Firefox with your app window.)

Option 2: testing in other browsers using Selenium RC

Selenium RC consists of a server and client libraries. There’s no actual installation necessary – you just need to download the jar file and appropriate client drivers from the Selenium download page and save them where you want. (Note that Selenium server version 1 onwards contains hudsuckr.exe, which caused a warning to pop up when I downloaded it on a machine running Kaspersky. There are some reports of this here and here.)

Setting up your tests

  • There are code examples in the Selenium documentation here, or if you’ve recorded tests in the IDE you can export them to C# by choosing File | Export Test Case As… | C# in the IDE menu.
  • You need to add the reference to the ThoughtWorks.Selenium.Core.dll from the relevant client driver folder.
  • You pass the name of the browser you want to test as a parameter when you instantiate the ISelenium object.
    • If the browser you want to test isn’t in the PATH on your machine you can specify the path in the same parameter e.g. @"*firefox C:\Program Files\Mozilla Firefox\firefox.exe"
    • To run tests in Safari 5 on Windows (XP and 7), I have to use *safariproxy instead of *safari (see here).
    • Likewise, I have to use *iexploreproxy to test IE8 on Windows 7, otherwise it keeps asking if the pop-up blocker is enabled. (There are some other options suggested on Stack Overflow, but I haven’t tried them.)
  • When working through the code example I did find I had to use google.co.uk instead of .com or I would get an error message when I called selenium.Open("/") when testing Safari.

Running Tests

  1. Start the Selenium server: from the command line, navigate to the folder and execute java -jar <server filename>.jar (you can put this in a bat)
  2. If you’re testing Safari, make sure the pop-up blocker is off
  3. Run your console app containing your tests

Reference

Advertisements

About Jennifer Phillips Campbell

Software Developer and Medieval Historian
This entry was posted in Testing. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s