How do we write test automation for ASP.NET?

Published Thursday, September 25, 2008 8:14 PM

Part of the responsibility of our team is to write automated regression tests for every ASP.NET feature. It is important to have a comprehensive test bed, since it is used for many years after a product version is released to verify any QFE or subsequent releases does not introduce regressions.

Our team uses an internal automation framework for all testing, and this week we released samples of how to use the "Lightweight Test Automation Framework for ASP.NET". You can find this release in the ASP.NET CodePlex page here. We are very excited to share our tools with the community. If you give it a try, tell us what you think.

What is included in this release?
This release contains samples that show how the ASP.NET QA team creates automated tests by using the "Lightweight Test Automation Framework for ASP.NET". The release is unsupported, and is provided in the form of samples only.
What is the Lightweight Test Automation Framework for ASP.NET?
This framework was developed and is currently used by the ASP.NET QA Team to automate regression tests for the product. It is designed to run within an ASP.NET application.
Tests can be written in any .NET Framework language. They use an API to manipulate and verify the DOM of the browser. It supports running tests in Microsoft Internet Explorer, Mozilla Firefox, Apple Safari, and Opera.
Note that even though it can be used to automate testing for any general-purpose Web application, its design was driven by the needs of the QA Team to automate ASP.NET regression tests.
How do I use the samples?

1. Download the Lightweight Test Automation Framework for ASP.NET Samples from the Web site and then open it in Visual Studio 2008. The Web site contains a small application and tests to automate it. The following illustration shows the Web site structure.


WebStructure.gif


2. Navigate to Home.aspx to interact with the Web site. To log in, use the user name ValidUser and use any string for the password. The page contains a GridView control that lets the user edit, delete, and insert data about school classes and grades. The following illustration shows a screen shot of the sample application.


SampleApp.gif


3. Navigate to \test\Default.aspx. This loads the testing UI, shown in the following illustration:


TestingUI.gif


4. Select tests from the tree view and then click Run Tests. The tests will run and display their progress in the right-hand frame.
5. To examine the test code, browse to the App_Code\Tests folder, and then open any file to inspect the test source code.

How do I debug test errors?
When a test fails, it is marked in red in the test tree. To examine the log, click the test name. The log information is displayed as shown in the following illustration:
Debug.gif
The log contains the stack trace of the test and the DOM that was captured by the framework at the moment of failure.
You can also have the test framework write a simple log to disk. To do so, select the Write Log to Disk check box before you run the tests. Note that this feature requires write access to the Web site’s root folder.
How do I use it with my own Web site?

1. Copy Microsoft.Web.Testing.Lightweight.dll into the Bin folder of your Web site. If you are testing a Web application, add the DLL as a reference to the project.
2. Copy Default.aspx and DriverPage.aspx into a folder in the Web site. By convention, the ASP.NET QA team uses the Test folder, but you can use any folder that you want. These are the pages that are required in order to run the tests.
3. If you are testing a Web site project, create tests in the App_Code folder. If you are testing a Web application project, you can create tests in any folder.
How do I use query-string arguments?
The framework accepts the following query-string arguments that are useful for filtering and running tests:
  • Tag=<tag_identifier>
    • Selects all tests that are marked with the WebTestTagAttribute and that have the specified identifier. For example, to automatically select a method that is marked with the [WebTestTag("DataScenarios")] attribute, you can navigate to the following URL:
http://server/site/test/Default.aspx?tag=DataScenarios
By default, the names of all classes that are marked with the WebTestClassAttribute and the name of all methods that are marked with the WebTestMethodAttribute are already identifiers.
    • You can specify multiple tag identifiers by concatenating them with the “@” symbol, as shown in the following example:
http://server/site/test/Default.aspx?tag=Pri0@MyTestMethod
This example shows how to automatically select all tests that have the [WebTestTag("Pri0")] attribute and additionally to select the test method named “MyTestMethod”.
  • Filter=[true|false]
    • Passing true causes all unselected tests to be removed from the test tree.
  • Run=[true|false]
    • Passing true will start running the selected tests on load.
Additional Resources
For more information, see the following resources:
  • The ASP.NET QA Team blog, where you can find additional samples.
  • The ASP.NET QA Road Map document, which can be found within the ASP.NET CodePlex page. This document describes upcoming features.
  • The ASP.NET CodePlex page, where you can post questions and feedback.

 

- Federico Silva Armas
ASP.NET QA Team

Comments

# funny wallpaper » How do we write test automation for ASP.NET? said on Friday, September 26, 2008 12:12 AM

Pingback from  funny wallpaper &raquo; How do we write test automation for ASP.NET?

# lkempe said on Friday, September 26, 2008 8:08 AM

Nice! Any plan to be able to run this in a Continous Integration environment?

And do you plan to have a recorder like watin test recorder ?

# Webshop-Winkelen said on Friday, September 26, 2008 11:05 AM

Does it also work for testing AJAX-enabled .Net web applications?

# britto said on Friday, September 26, 2008 12:29 PM

Nice and simple to use framework. Would like to see more samples and documentation. Keep up the good work.

# Sasha B said on Monday, September 29, 2008 3:53 AM

Is it possible to run these test in a something like Nunit? So that we could run tests without using the test page.

# korayem said on Tuesday, September 30, 2008 7:43 AM

Excellent work guys, this is awesome.

# farmas said on Saturday, October 04, 2008 11:11 PM

lkempe: we do run tests that are written with this framework as part of a continous integration machine. We wrote a small tool that automates the creation of the vdir, then launches a browser pointing to the test driver page passing "?log=true&run=true" as the query string parameters to automatically run and write the log to disk. We can release this tool after we publish the framework sources.

As for a recorder, we do not have plans to create one.

- Federico

# farmas said on Saturday, October 04, 2008 11:16 PM

Webshop-Winkelen: Yes, this framework supports testing web sites that use Ajax, that is one of the primary reasons why this framework was created. If you look through the API there are a lot of "Wait" methods that are useful when automating XmlHttp requests. For example: HtmlPage.WaitForAsyncPostComplete, HtmlPage.WaitForScript, HtmlElement.WaitForAttributeValue, HtmlElement.WaitForInnerText, HtmlElement.WaitUntilNotFound.

- Federico

# farmas said on Saturday, October 04, 2008 11:17 PM

britto: Thanks! I am working on creating more samples of how to use our test framework. I am also looking forward of releasing the source code, unit tests and documentation as soon as I can :)

- Federico

# farmas said on Saturday, October 04, 2008 11:23 PM

Sasha B: This a functional automation framework, and is not intended to be used for unit tests. I am afraid you can't integrate them nicely with NUnit.

- Federico

# Dominic said on Monday, October 27, 2008 8:50 AM

I am having problems with the framework and Firefox. I create a new page with new HtmlPage(url);

and it never returns in FF. Is there something else I need to set?

# osbornm said on Wednesday, October 29, 2008 2:34 PM

@Dominic Can you please elaborate what you mean by "never returns in FF"

# Dominic said on Thursday, October 30, 2008 8:21 AM

Firefox seems to be working now, it was more to do with my installation as other things were breaking as well.

I have a problem at the moment in that the iste I am testing uses JQuery for AJAX. And the DOM is added to from an AJAX callback but I can't find a way to pick up the DOM changes. If I do a refresh through navigation I lose the new DOM elements, I can't wait for Async Postbacks as they never get picked up, neither does WaitForDomChange and even refreshing the Elements of the page isn't picking up the new items even though they are appearing in the page.

Is there another way to test if the DOM has been updated?

# Steve Haeney said on Wednesday, November 05, 2008 10:58 AM

This is all good stuff, but is there a forum where early adopters of this framework can exchange knowledge?

I don't think this page is the place for in-dept discussions!

# osbornm said on Thursday, November 06, 2008 2:29 AM

@Dominic there is a method on HtmlElement called WaitForInnerText("<value>",<Timeout>) You could use this to monitor a element you know will be updated.

# osbornm said on Thursday, November 06, 2008 2:30 AM

@Steve Haeney Currently there is not one but our  Codeplex site would be the best place for advanced, in-depth discussions.

# DM said on Friday, November 14, 2008 3:51 AM

Is there a way to access Session variables from tests methods? (Need to fill a custom captcha verification field, and it would be handy to collect the value from Session variables).

Thanks for your time, you built an awesome tool,

DM

# osbornm said on Saturday, November 29, 2008 11:04 PM

@DM there is currently no way to do this.  

# Dave R said on Thursday, January 15, 2009 2:23 PM

Great tool!  

Do you have any additional samples working with gridViews, such as invoking the pagination functionality?

Dave

# farmas said on Wednesday, January 28, 2009 12:31 PM

@Dave Here is one example of what you may do:

// Get the gridview table

HtmlTableElement gridView = (HtmlTableElement)page.Elements.Find("CoursesGridView");

// Go to page two

gridView.Rows[11].ChildElements.Find("a", "2", 0).Click(WaitFor.AsyncPostback);

It is also possible to build your own abstractions on top of the framework. For example, we built a custom GridViewTest class that helps us automate that control. For example:

TestGridView gridView = new TestGridView(page.Elements.Find("GridView1"));

gridView.Page("4");

- Federico

# Sean said on Thursday, February 12, 2009 1:07 PM

Just found this framework today and LOVE it but I'm having trouble finding a way to look at a ValidationSummary server side conrtol to see the errors that were added. I can find the control using HtmlPage.Elements.Find("") but how do I see the text/html?

Thanks

# farmas said on Thursday, February 12, 2009 7:23 PM

@Sean: Here is a sample test to verify the text of the validatiosummary:

HtmlPage p = new HtmlPage("Login.aspx");

       p.Elements.Find("Button1").Click();

       string errorMessages = p.Elements.Find("ValidationSummary1").GetInnerTextRecursively();

       Assert.StringContains(errorMessages, "Name is Required");

       Assert.StringContains(errorMessages, "Password is Required");

# farmas said on Thursday, February 12, 2009 7:30 PM

UPDATE:

We have released an update of the lightweight automation framework to CodePlex that includes source code. We have also created a forum where you can ask questions or leave feedback regarding this framework.

Forum is here: forums.asp.net/1193.aspx

Thanks,

Federico

# Sean said on Monday, February 16, 2009 8:36 AM

farmas - Thanks! I swear I thought I tried this last week but it works!

# said on Tuesday, February 24, 2009 6:23 AM

El equipo que testea la calidad de los productos liberados de ASP.NET (parece que es algo as&iacute;&hellip;

# Jose A. Fernandez said on Tuesday, February 24, 2009 6:23 AM

El equipo que testea la calidad de los productos liberados de ASP.NET (parece que es algo as&iacute;&hellip;

# DCYAN said on Wednesday, May 06, 2009 8:41 AM

When I use it with my own Web site, i encounter the following problem

HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)

Can you tell me how to resolve it?

# farmas said on Wednesday, May 06, 2009 11:18 PM

I have never seen this before. Could you start a thread over in our forum and provide more information so that we can look into it?

forums.asp.net/1193.aspx

Thanks,

Federico

Leave a Comment

(required) 
(required) 
(optional)
(required)