.comment-link {margin-left:.6em;}
Books & Articles I wrote.

Monday, August 07, 2006


Web Tests with Team Suite 2005

Today I had to write some web tests in Visual Studio Team Suite 2005. It isn't overly intuitive to get started, but after using it for an hour or so i started to really get how it would be useful and how pattern orientated it was. The solution itself isn't based around patterns, but almost everything you do have a base pattern to it with some extensions based on what you specifically want to test. Now i've started i can see myself using it all over - te difficulty in getting started was that the place i am doing some work for at the moment use Anthem and so there is a bunch of attributes and properties that need to be set to get things working.

Making sure you are using Team System for Testers, add the following to your test class:

using Microsoft.VisualStudio.TestTools.WebTesting;
using Microsoft.VisualStudio.TestTools.WebTesting.Rules;

Next, derive your class from WebTest:

public class MyWebTest : WebTest

You can state whether to authenticate requests via the PreAuthenticate property in the constructor.

public AlertWebTest()
this.PreAuthenticate = true;

Finally, override the GetRequestEnumerator() where you build the tests that will be evaluated.

public override IEnumerator GetRequestEnumerator()

At this point, put yourself in userland. If you click using a postback or anthem (or Atlas), you need to simulate it in code. So let's initialize my page and capture the ViewState - this is needed to allow the application to flow as it would in a browser - keep in mind the things that happen underneat the covers (such as hidden variables) and relate these to your code.

WebTestRequest defaultRequest = new WebTestRequest("http://domain.com/default.aspx");
defaultRequest.ThinkTime = 10;
defaultRequest.Method = "POST";
FormPostHttpBody defaultRequestBody = new FormPostHttpBody();
defaultRequest.Body = defaultRequestBody;

//get the hidden variables, such as ViewState into context
ExtractHiddenFields rule0 = new ExtractHiddenFields();
rule0.ContextParameterName = "0";
defaultRequest.ExtractValues += new EventHandler(rule0.Extract);

yield return defaultRequest;

Notice the use of ExtractHiddenFields to get the viewstate into a context you can later refer to. Also, the yield statement adds this request to the enumerable collection of webtests that will be evaluated.

Now, we have the page loaded and Anthem initialized, we can simulate clicking a link which will call an Anthem method to update the page with some data.

//now click the inbox link
WebTestRequest getRequest= new WebTestRequest("http://domain.com/default.aspx");
getRequest.ThinkTime = 7;
getRequest.Method = "POST";

FormPostHttpBody inboxRequestBody = new FormPostHttpBody();
inboxRequestBody.FormPostParameters.Add("Anthem_UpdatePage", "true");
inboxRequestBody.FormPostParameters.Add("__EVENTTARGET", "ctl00$PanelView1$LinkButton2");

getRequestBody.FormPostParameters.Add("__EVENTARGUMENT", "");

//set the request
getRequest.Body = getRequestBody;

//create a validation rule to check whether we have "my text" anywhere
ValidationRuleFindText myTextRule = new ValidationRuleFindText();
myTextRule.FindText = "my text";
myTextRule.IgnoreCase = false;
myTextRule.UseRegularExpression = false;
myTextRule.PassIfTextFound = true;
getRequest.ValidateResponse += new EventHandler(alertTextRule.Validate);

yield return getRequest;

Notice this time we need to pass some Anthem related stuff as it isn't a diect ASP.Net postback. Finally notice probably the coolest part which allows you to add validation rules that are evaluated and pass or fail the test - in my case i am just asking whether "my text" is in the output response, although it can be much more complex than that.

Very cool work in any case, but there is a bit to be done to make it more friendly i think. However, is does take the old NUnit style of working to a new level and will soon be a critical part of testers - and developers - toolkits.

This page is powered by Blogger. Isn't yours?

Weblog Commenting and Trackback by HaloScan.com