Geeks With Blogs

@woodbasen
  • woodbasen Man må bare elske Fars dag! ungerne laver morgenmad. En gave der garanterer en perfekt @COPENHELL. En halv arbejdsdag og en fodboldkamp :-D about 451 days ago

Woodbase Umbraco - CSS - jQuery - .Net - C#

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:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sample Selenium Test Suite</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td>Test Cases for De Anza A-Z Directory Links</td></tr>
</thead>
<tbody>
<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>
</tbody>
</table>
</body>
</html>

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

[TestFixture]
public class MyLinkTester{
.......
}

 

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

Selenium client;
StringBuilder verificationErrors;
[SetUp]
        public void TestSetup()
        {
            client = new DefaultSelenium("localhost", 4444, "*iexplore", 
                                                 "http://www.mydomain.org");
            client.Start();            
            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:

[Test]
        public void TestForLinkB()
        {
                client.Open("http://www.mydomain.org/Default.aspx");
                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:
[TearDown]
        public void TestTearDown()
        {
            try
            {
                client.Close();
            }
            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: www.selenium.org
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

Your comment:
 (will show your gravatar)
 


Copyright © woodbase | Powered by: GeeksWithBlogs.net | Join free