Web application testing using WatiN and C#


Today I got a chance to explore web application testing with WatiN library. You can download WatiN from website. Or you can install via NuGet, using following command.

Install-Package WatiN

I am using VS Express 2012 for Web, for testing my web application. I am testing a Login page, where I got two textboxes for username and password, one button for submitting the values, two required field validators to validate username and password textbox, validation summary control to show validation messages.

Currently WatiN supports both Internet Explorer and Firefox browsers. In this post I am using only internet explorer browser.

Here is my server side code, which will check username and password, and display message in a label.

if (txtUsername.Text.Equals("username") && txtPassword.Text.Equals("password"))
    lblInfo.Text = string.Format("Welcome {0}", txtUsername.Text);
    lblInfo.Text = "Authentication failed";

Here is the TestInitialize and TestCleanup, these methods will be executed while before running test and after running test, used to initialize and cleanup variables, objects etc which is required for the tests.

private IE _ieBrowser;

public void Setup()
	//Creates an instance of IE browser.
    _ieBrowser = new IE("http://localhost:53574/login.aspx");

public void Shutdown()
	//Closing the browser and disposing the object.

And here is the test, which will provide the username and password, and verifies the message from the web page.

public void VerifyAuthenticationWithValidCredentials()
	//Find textbox with Id equals txtUsername and type text "username"
	//Find textbox with Id equals txtPassword and type text "password"
	//Find the button with id cmdSubmit, and click on the same button.
	//As ASP label controls are rendered as SPAN, look for the Span control with Id lblInfo, 
	//and get the inner html, which will be the content.
	var message = _ieBrowser.Span("lblInfo").InnerHtml;
	//And verification part.
    Assert.AreEqual("Welcome username", message);

Here is another test, in this, I am not providing the password and verifies I am getting a validation alert from the web page using WatiN.

public void VerifyJavascriptValidationForPassword()
	//The alert handler object helps 
	//to get the javascript alerts from webpage.
    var alertDialogHandler = new AlertDialogHandler();
    using (new UseDialogOnce(_ieBrowser.DialogWatcher, alertDialogHandler))

And here is the screenshot of the test results window.

Test Explorer Window

Test Explorer Window

It is a very basic introduction to Web application testing. There are more tools available in the market.

Happy Programming :)

Anuraj is currently working as Technical Lead. He is a regular speaker on Technology events. He has more than 11+ years experience with Microsoft.Net Technologies.

Facebook Twitter LinkedIn Google+ 

  • http://www.acatlan.unam.mx tonovarela

    this code can test in aspx c#?
    Im trying this in my web page
    Can you help me
    my email is tonovarela@live.com

    • http://www.dotnetthoughts.net Anuraj P

      Yes this code works with ASP.Net C#. What help you require? Please let me know.