Saturday, July 30, 2011

Test application for a web project

Once we have a fully functional website it is very common to add features to it without testing it thoroughly enough and getting on with the release. This can leave some bugs unnoticed which could prove to be embarrassing for a developer. However with applications that perform automated testing for you, you can sit back and relax while your application does the testing and gives you the results.
Thanks to WatiN it is not only possible but very easy as well. For an introduction to WatiN you could go to WatiN Home and learn more about it.

Anyways, to create a Hello World application in WatiN you need to create a new console application in .NET.
In that application right click on References (which can be found in the Solution Explorer), and click add Library package reference, over there you can search for WatiN and download it. If you do not have the option of adding a library package reference, you can download it manually from here and add it to your project.
Once this is done, you can use the following code to create a basic application that will go to Google, search for "Hello World" and click the search button to get results.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WatiN.Core;


namespace WatiNTest
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            IE browser = new IE("http://www.google.com");
            browser.TextField(Find.ByName("q")).TypeText("Hello World");
            browser.Button(Find.ByName("btnG")).Click();
        }           
    }
}

In this code, there is a keyword [STAThread] on top of void main, this is essential for our application to successfully communicate with Internet Explorer.
In the first line of code, I created a new instance of Internet Explorer (browser), and opened google in it.
In the second line of code, I used WatiN's built in function to find the text box named "q", and type "Hello World" in it. The question here is how I found out the name of google's search textbox. Simple, if you open google on Internet Explorer and press F12, you will open Internet Explorer's development tools. The page should now look like this:

 


In developer tools, there is an icon under the tab "HTML" that looks like a cursor of a mouse. If you click on it, and then click on the google search textbox, it will give you a lot of information about that textbox like this:

 

Notice here that the information about that textbox is highlighted in blue. If you go through this, you can see that the name of this textbox is "q".

Once you have done this, you can now insert anything in the search box, which in the example given above is "Hello World". After that if you follow the same technique, you can find the name of the button "google search" and click it (as done in the example).
 
There you go, your hello world testing application is now complete. You can add more things to it, such as after the code you can add a line of code that clicks the button "next page" and shows page 2 of results. (WatiN has been designed in a way that if you click "google search" and in the next line you write the code to click "results page 2" button, it will click "google search" wait for the page to load and once it is loaded click on page 2 button).

I hope it works without any problems, however, you do not need to worry if you face problems doing it. My next post will focus more on them.

No comments:

Post a Comment