Web application testing using WatiN and C#

Standard

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);
}
else
{
    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;

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

[TestCleanup]
public void Shutdown()
{
	//Closing the browser and disposing the object.
    _ieBrowser.Close();
    _ieBrowser.Dispose();
}

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

[TestMethod]
public void VerifyAuthenticationWithValidCredentials()
{
	//Find textbox with Id equals txtUsername and type text "username"
    _ieBrowser.TextField("txtUsername").TypeText("username");
	//Find textbox with Id equals txtPassword and type text "password"
    _ieBrowser.TextField("txtPassword").TypeText("password");
	//Find the button with id cmdSubmit, and click on the same button.
    _ieBrowser.Button("cmdSubmit").Click();
	//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.

[TestMethod]
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))
    {
        _ieBrowser.TextField("txtUsername").TypeText("username");
        _ieBrowser.Button("cmdSubmit").ClickNoWait();
        alertDialogHandler.WaitUntilExists(20);
        alertDialogHandler.OKButton.Click();
    }
}

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 :)


  • 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
    Thanks

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

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