About/Contact

Steve Trefethen

Steve Trefethen is CTO at Wanderful Media.
Contact me

View my LinkedIn profile



Calendar

<<  May 2013  >>
MoTuWeThFrSaSu
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar

Disclaimer

The posts on this weblog are provided AS IS with no warranties, and confer no rights. The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.



Automated testing of ASP.NET web applications using Selenium

July 31 2007 8:06AM

Lately, I've been focused on Web Application testing frameworks and I've been looking at a number of different options and having varying degrees of success. That is, of course, until I started looking at Selenium after Lino mentioned it to me and I'm very impressed. Selenium is an open source project with multiple tools designed for testing web applications. Selenium includes:

  • Selenium IDE an add-on to Firefox use for recording test scripts
  • Selenium RC (Remote Control) a Java server used for execution of test scripts
  • Selenium Core client side testing support for web applications added directly to your application
All of these are interesting in their own right so be sure to look closely at each piece.

Selenium IDE

Selenium IDE
The Selenium IDE is a non-modal dialog add-on for Firefox that supports Selenium test development. In fact, it's more than fair to call this single dialog an IDE because it fully supports recording, development and debugging of test scripts. Additionally, it can format test scripts in any of the following for use with Selenium RC:
  • HTML
  • Java
  • C#
  • Perl
  • PHP
  • Python
  • Ruby

A Selenium test script consists of a series of Commands which have a Target and optionally a Value. For example, browsing to a web page using Selenium consists of the following:

Command: open
Target: http://localhost/myapp

To click on a link titled "About" on a page the Command might look like this:

Command: clickAndWait
Target: link=About

This will initiate the click and wait until the new page is done loading. There are literally hundreds of commands to choose from covering input (both keyboard and mouse), control manipulation, drag/drop, evaluation, verification, waiting, browser manipulation and just about anything else you'll need.

If you've done any UI testing at all you're familiar with the challenge of manipulating a UI programmatically in a manner independent from of the size, position or location of the control you're trying to manipulate. Selenium solves this problem using XPATH and providing the ability to locate controls based on XPATH expressions alleviating the need to hard code HTML tag structure into a test script. This is particularly crucial for ASP.NET testing since the runtime mangles the ID attributes of rendered tags. For example, the ASP.NET runtime may render ID attributes that look like:

id="ctl00_cphContents_gridMaint_DataGrid"

Finding this control using an XPATH expression can be simplified to something like this:

//table[contains(@id, "gridMaint")]

In the event the nesting of the DataGrid changes the script will continue to function properly as long as table's ID contains the text "gridMaint".

Example of using the Find button on the Selenium IDE

Notice the link is highlighted in the browser

Playing back tests using Selenium TestRunner

The Selenium IDE has a toolbar button that will launch a feature called TestRunner which allows you to playback your tests using controls hosted in an IFRAME inside your browser. Here's what it looks like:

Selenium TestRunner

You can playback your entire test from right within your browser. It's like having the IDE built right into your application.

Selenium RC

Next, is Selenium RC which is a Java server application used execute Selenium tests and drive a browser instance through AJAX communication with the browser. To start Selenium RC simply execute the following from a command prompt:

java -jar selenium-server.jar -interactive

Since I'm focused on .NET I'll discuss the C# approach. Once you've recorded and debugged your test you can capture it as C# and compile it into an NUnit compatible assembly where upon execution it will drive Selenium RC to manipulate the web application through a browser instance running on your desktop. Here is an example of the above recorded test in C#:

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using Selenium;

namespace SeleniumTests
{
    [TestFixture]
    public class NewTest
    {
        private ISelenium selenium;
        private StringBuilder verificationErrors;
        
        [SetUp]
        public void SetupTest()
        {
            selenium = new DefaultSelenium("localhost", 4444, "*firefox",
                              "http://localhost:4444");
            selenium.Start();
            verificationErrors = new StringBuilder();
        }
        
        [TearDown]
        public void TeardownTest()
        {
            try
            {
                selenium.Stop();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }
            Assert.AreEqual("", verificationErrors.ToString());
        }
        
        [Test]
        public void TheNewTest()
        {
            selenium.Open("/selenium-ide/");
            selenium.Click("link=About");
            selenium.WaitForPageToLoad("30000");
        }
    }
}

Generation of NUnit compatible tests is great because it makes it really easy to incorporate Selenium test suites into a continuous integration environment like CruiseControl.NET.

Conclusion

If you're in need of a web application testing framework you owe it to yourself to check out Selenium. I'd go so far as to say if you're doing doing any web related development and you're not already using Selenium you should stop right now and go download it. Here are there RSS feeds.

After playing with Selenium for about an hour I was easily able to:

  • Record tests against a RIA
  • Build a C# NUnit compatible assembly to drive Selenium RC
  • Integrate the tests into an automated build using CruiseControl.NET

Since then I've been able to really dig in and accomplish a lot of work in a very short period of time. The next step is to build up a nice battery of tests and dig into NCover to help figure out where the holes are.

Lastly, I just want to tip my hat to the ThoughtWorks and volunteers and supporters of the Selenium project. Kudos for such a great framework!

FacebookDel.icio.usDigg It!

Comments (11) -

8/1/2007 10:47:03 PM #

There's also NUnitASP (http://nunitasp.sourceforge.net/) which is good for testing server side objects in depth. Don't Forget FIT (c2.fit.com) which has a nice engine for .NET. Combine FIT and NUnitASP along with NUnit and you can do some fairly robust testing (not to mention FITNesse).

Anyways just my two cents.

Mark Vejvoda

8/2/2007 2:31:02 AM #

First things first, if you are involved in web development, get a Web Application testing tool.
So thanks for this post, Steve.

For a different approach of what can be done with and for the .NET platform,
we created http://www.incisif.net" title="InCisif.net" />.
- Record mode,
- Very different api from Selenium,
- C#, VB.NET and IronPython support
- Visual Studio 2003, 2005, 2008, Express Edition support
- NUnit and VS 2008 test framework support

Don't be afraid to try!

FTorres

8/2/2007 6:01:03 AM #

Mark,
Thanks for the NUnitASP link. I did find when Googling but this quote from the bottom of that page would have me a bit concerned "In 2006 and 2007, NUnitAsp languished as James turned his attention to other projects. The v2.0 release, published in June 2007, will be the last release James produces. He is looking for someone else to take over; if you would like to do so, please post to the nunitasp-devl mailing list." Selenium forums are very much alive and kicking and the repository is active with code changes which is very encouraging.

FTorres,
Thanks for the comment. I did find that site when looking for tools though the OS choice/price point is very attractive especially considering how well it works. That said, I've watched a few of the incisif videos and the tool looks interesting.

Steve Trefethen

9/11/2007 11:28:31 AM #

Hello,

Can you help me with a CCNet + MSBuild + email config?
When my build fail, I need error message in email, but I don't know how to put it.

Regards

Zote

9/21/2007 3:49:04 AM #

Hi Zote,
  This page describes what you need to add to your ccnet.config to get email publishing working.

confluence.public.thoughtworks.org/.../Email+Publisher

Steve Trefethen

7/4/2008 10:44:05 AM #

hi!,
i am using selemium IDE from past one month .I want to shift to selenium RC /IDE integrated with nunit . C# with nunit....but i don't know how to do....n hw i will interate nunit....could u plz help me .how i can use selenium RC (C# n nunit).
waiting for u reply
regards

amita

9/10/2008 10:40:06 AM #

Great way to setup selenium for asp.net under windows platform. Here is a simple way to use selenium under windows platform for php. cyberheight.blogspot.com/.../...dows-platform.html

David

11/3/2009 4:41:56 AM #

Excellent article. Just what I was looking for

Sun United States

11/23/2009 5:00:27 PM #

Great post.

waggi United States

12/9/2009 1:32:46 PM #

I used Selenium to automate smoke test for big business web applications and shared my experience here: slmoloch.blogspot.com/.../...tests-for-aspnet.html

Yauheni Sivukha Belarus

1/21/2010 3:34:04 AM #

This is informative post for those who do the automation testing.
I am user of QTP and would like to know more about the Selenium also would like to know how selenium use for to Test Web application. if  anybody having  documents / tutorials or ref link will help.

Also would like to know ,what will be the feature wise diff in QTP and Selenium? and which the best to test Web application?

Thanks
Sandeep

sandeep India

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading