Geeks With Blogs
Woodbase Godot - Blender - .Net - C# - Game Development

Most people can agree that testing your software is a good thing. Most developers will agree that unit testing is a great thing to have.

But how about testing your web site? Fair enough you can run unit tests on your code behind, but unit testing a GUI can be problematic.

If you struggled with this problem, don't worry - you're not the first!

I've recently become aware of Selenium. Selenium is a very powerfull tool for testing web applications. It gives you a lot of nice features to help you test your web site.

There's even a plug-in for Firefox to let you record your test and then simply play them, whenever you need to test your site. The plugin can also translate the recorded test into ordinary unit tests, so that you can integrate it with your other unit tests.

A simple case:
You wish to test if your page shows a specific link:

Page markup:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sample Selenium Test Suite</title>
<table cellpadding="1" cellspacing="1" border="1">
<tr><td>Test Cases for De Anza A-Z Directory Links</td></tr>
<tr><td><a href="./a.html">A Links</a></td></tr>
<tr><td><a href="./b.html">B Links</a></td></tr>
<tr><td><a href="./c.html">C Links</a></td></tr>
<tr><td><a href="./d.html">D Links</a></td></tr>

Now in order to test this we need to write some test code:

First we need to import NUnit and the Selenium framework:

using NUnit.Framework;
using Selenium;

Then we need to set the class up to be a TestFixture

public class MyLinkTester{


Then we need our set up method in order to instanciate a selenium server instance.

Selenium client;
StringBuilder verificationErrors;
        public void TestSetup()
            client = new DefaultSelenium("localhost", 4444, "*iexplore", 
            verificationErrors = new StringBuilder();

The client line might need a little explanation. A DefaultSelenium requires a server to run. A Selenium server is provided with the Selenium RC (Remote Control) and needs to be started for the tests to run.

Second parameter is the port number of the server default 4444.

Third is the name of the browser you wish to test you web site on. Selenium supports a wide range of web browsers from Safari to IE.

Final parameter is the location of you web site. Once it's started we're ready to run our tests.

A simple test for the markup above could be:

        public void TestForLinkB()
                bool result = client.IsTextPresent("B Links");
                Assert.IsTrue(result, "Testing B Links");

This will open the Default.aspx page in a browser and test if the text "B Links" can be found.

The outcome determens wether your test passes or fails.

Now the only thing we need is to close it all:
        public void TestTearDown()
            catch (Exception ex)
                verificationErrors.AppendLine("Failed to close client\n" + ex.Message);
            Assert.AreEqual("", verificationErrors.ToString(), 
                                         "Checking if any errors occured");

Now this was a very simple example. Selenium provides much more advanced features for testing if links take you to the right location or if input forms validate correctly and so on.

Selenium can be your test person - once you've recorded or written your tests.

For more information on Selenium please refer to their web site:
Posted on Tuesday, October 6, 2009 2:37 AM | Back to top

Comments on this post: Testing web sites

# re: Testing web sites
Requesting Gravatar...
I have been using selenium for tha last month to test an existing application. It has saved so much time that I would have spent doing manual testing. I have to test all types of credit card transactions so I can easily modify the credit card number and try again.
Left by denise on Oct 06, 2009 7:09 AM

# re: Testing web sites
Requesting Gravatar...
This is a basic and important step to do first. - Paradise Home Improvement Charlotte
Left by Jen Walkins on Dec 10, 2016 6:35 AM

Your comment:
 (will show your gravatar)

Copyright © woodbase | Powered by: