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

  • Mastering Blazor Authentication and Authorization

    At the Visual Studio Live! @ Microsoft HQ developer conference set for August, Rockford Lhotka will explain the ins and outs of authentication across Blazor Server, WebAssembly, and .NET MAUI Hybrid apps, and show how to use identity and claims to customize application behavior through fine-grained authorization.

  • Linear Support Vector Regression from Scratch Using C# with Evolutionary Training

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the linear support vector regression (linear SVR) technique, where the goal is to predict a single numeric value. A linear SVR model uses an unusual error/loss function and cannot be trained using standard simple techniques, and so evolutionary optimization training is used.

  • Low-Code Report Says AI Will Enhance, Not Replace DIY Dev Tools

    Along with replacing software developers and possibly killing humanity, advanced AI is seen by many as a death knell for the do-it-yourself, low-code/no-code tooling industry, but a new report belies that notion.

  • Vibe Coding with Latest Visual Studio Preview

    Microsoft's latest Visual Studio preview facilitates "vibe coding," where developers mainly use GitHub Copilot AI to do all the programming in accordance with spoken or typed instructions.

  • Steve Sanderson Previews AI App Dev: Small Models, Agents and a Blazor Voice Assistant

    Blazor creator Steve Sanderson presented a keynote at the recent NDC London 2025 conference where he previewed the future of .NET application development with smaller AI models and autonomous agents, along with showcasing a new Blazor voice assistant project demonstrating cutting-edge functionality.

Subscribe on YouTube