Tags

, , , , , , , , , , , , , ,

Lately I have been having troubles with the “unique” ways of automation testing in Visual Studio 2010 Ultimate. When you create a Web performance test, you can record your actions, and then leave the computer to do them automatically when running these tests. You can also use some of the validation rules to check if the site’s content is as you expect. But these built=in rules often don’t satisfy your need. That’s where the custom rules come in handy.

Class implementation


To create our own cutsom validation rule, first off, we need a new class, in which to write our rule(make sure this class is public). After we create this class we need to implement the interface ValidationRule for this class. I should probably note that to use this interface you need to add the VS WebTesting namespace (“using Microsoft.VisualStudio.TestTools.WebTesting;”) Implementing this interface only means to add this method to our class:

public override void Validate(object sender, ValidationEventArgs e) {}
To pass parameters to our new Validation rule(like the Ignore Case property of the Find Text rule), you just need to create public properties in this class.

And some validation is needed too, after all

The most important part in our Validate() method are the ValidationEventArgs. The e.IsValid property is the property that decides if the validation has failed or passed. Other important property is the e.Response, which contains the response(web page, json, xml, etc.) of the server that we are testing. You will probably need most the e.Response.BodyString property, which contains the body of our document(html, xml, …) as a string, but there are other properties which you might find interesting, like the e.Response.Cookies containing your favourite html snack.

Some final touch

If you like to have your custom Validation rule to have normal names and description, you might want to add some descriptive attributes. Just include the namespace System.ComponentModel and put this above your methods/class which you need described:
[DisplayName(“Some validation property”), Description(“The text that describes this property.”)]
Of course, you can have only one attribute or none above your methods or class.

Example- Validation rule for search result


You can see mine custom Validation rule here. This rule uses regex expressions set by the user to get the search results from the html page, then searches the string of the search result for at least one occurrence of each searched word.
Well, that is it for this topic. Have fun with the automatic testing in VS as you have a little more freedom with the Validation rules.

Advertisements