Unit testing aspx pages with WatiN tool

I just read a post on GridViewGuy blog that introduces a great tool for testing aspx pages - WatiN, and it really grabbed my attention. To put it in short, WatiN is a framework that will let you write unit test in order to test aspx pages functionality.

"Inspired by Watir development of WatiN started in December 2005 to make a similar kind of Web Application Testing possible for the .Net languages. Since then WatiN has grown into an easy to use, feature rich and stable framework. WatiN is developed in C# and aims to bring you an easy way to automate tests with Internet Explorer."

waitin

WatiN is written in C# and is licensed under Apache License 2.0. The important features are the support for Ajax pages, frames and iframes and popup dialogs. It supports testing in Internet Explorer 6, 7 and 8 and Firefox 2 as well.

Now let's see some example. Take a look at the sample code that will search for keyword "MSForge" on Google in both IE and Firefox browsers.

[Test]
public void SearchForMSForge() 
{ 
    using (IBrowser ie = BrowserFactory.Create(BrowserType.InternetExplorer)) 
    { 
        ie.GoTo(http://www.google.com); 
        ie.TextField(Find.ByName("q")).Value = "MSForge"; 
        ie.Button(Find.ByName("btnG")).Click(); 
        Assert.IsTrue(ie.ContainsText("MSForge")); 
    } 


    using (IBrowser firefox = BrowserFactory.Create(BrowserType.FireFox)) 
    { 
        firefox.GoTo(http://www.google.com); 
        firefox.TextField(Find.ByName("q")).Value = "MSForge"; 
        firefox.Button(Find.ByName("btnG")).Click(); 
        Assert.IsTrue(firefox.ContainsText("MSForge")); 
    } 
}

As you can see it is similar to "classic" unit testing. Test methods should have [Test] attribute and have to return no data. You simple create a new instance of a browser and access any DOM element. After simulating a search behavior of a user, we can examine the results by well known Assert class.

You can find more information on WatiN website and download WatiN package with binaries and samples from here.

Happy testing :)

Bookmark and Share

Published Thursday, March 27, 2008 12:15 PM by janko
Powered by Community Server (Commercial Edition), by Telligent Systems