Inside VSTS

Testing AJAX Applications with VSTS 2008

Jeff walks you through the new method of validating AJAX code in Visual Studio Team System 2008 (no Fiddler required!).

Many Visual Studio Team System 2008 improvements are focused on testers: Most of the improvements are in the Tester and Developer editions, and focus around code metrics, Web testing, load testing and performance profiling. This article focuses on one of the new features of Web testing: the ability to test AJAX Web sites.

In version one of Team System, if you tried to record a Web test on a site that had AJAX sections, the calls to the server were not recorded. To record them you had to use Fiddler and import the resulting script into Visual Studio. Many people didn't know about this solution; those that did realized it was problematic at best.

For this article I've created a simple Web page which has a Script Manager and an Update Panel. In the Update Panel there is a textbox which accepts a value, a submit button and a label which displays "Welcome " plus the value entered in the textbox. This is processed on the server via a postback in the button click event (see Figure 1 for a screen shot).

Figure 1
[Click on image for larger view.]
Figure 1. Example AJAX App

When the test is recorded, four different URLs are listed: the original default.aspx page call, two ScriptResource.axd calls (the browser calling for the javascript to be loaded on the client), and a last call to default.aspx that is made when you click the submit button. So far, so good -- but we haven't validated that the page worked the way it was supposed to work. So how do you validate data that comes back from an update panel (or any XMLHttpRequest)? It turns out this is a bit more difficult to do.

If you look at the results of the test, specifically for response from the submit button, you will see the text in Listing 1. This is obviously not the most helpful information for validating that the text you wanted displayed was displayed. You cannot use a form field validation because the data that comes back is not in the form of a valid HTML document. In fact, there is only one way to validate an AJAX response with the built in validation rules: Use the FindText validation rule and search for the value you entered. Or you could write your own…

Create a new Class Library project and add a reference to Microsoft.VisualStudio. QualityTools.WebTestFramework. The code for this validation rule is shown in Listing 2. Without going into too much detail about the class, you should focus on the regular expression: "(?<field>id=\"" + _control + "\".*>)(?<value>.*)<"

While this isn't a tutorial on regular expressions, it's important to understand what's happening here in case you need to make changes (I will caveat this by saying that this has worked with all of my tests so far, but there is always a possibility you will come across a situation I haven't!). The "(?<field>" part of the regular expression says to find (or capture in regex parlance) text which starts with "id=" + the name of the control you enter, and any characters which come after until a right arrow (">") is found in a group called field. The next part "(?<value>.*)<" says to capture any text which comes after the right arrow and end at the next left arrow ("<"). The text captured (excluding the left arrow) is stored in another group called value.

The code simply tries to match this expression to the response. You could even change this to actually look for the value as part of the regular expression and, if no matches occur, just set the IsValid to false. To do this you could change the regular expression to "(?<field>id=\"" + _control + "\".*>" + _value + "<" and re-write the Validate method as shown in Listing 3.

To implement the validation rule, simply add a reference to the assembly to the project which contains your Web tests and it will show up in the list of validation rules.

This simple validation rule plus the ability of Visual Studio Team System 2008 to record AJAX requests and responses should enable you to quickly and easily validate the results of your AJAX tests.

About the Author

Jeff Levinson is the Application Lifecycle Management practice lead for Northwest Cadence specializing in process and methodology. He is the co-author of "Pro Visual Studio Team System with Database Professionals" (Apress 2007), the author of "Building Client/Server Applications with VB.NET" (Apress 2003) and has written numerous articles. He is an MCAD, MCSD, MCDBA, MCT and is a Team System MVP. He has a Masters in Software Engineering from Carnegie Mellon University and is a former Solutions Design and Integration Architect for The Boeing Company. You can reach him at [email protected].

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube