Monday, May 14, 2007 12:36 PM
kolbis
How to abort a web test on specific request
By default all requests inside a web test will be executed no matter of the results. In other words, if you got 2 requests inside your web test and the first request fails, the web test will continue to the second request and will execute it.
Sometimes, we do not want to continue running the following requests upon a failure. To do so, we can choose to code our web test or to create a WebTestRequestPlugIn.
Coded web tests are to be avoided as possible and there are only three cases where I would consider using one:
- Branching - An IF...Else
- Looping
- Javascript / ActiveX
I am not going to elaborate about coded web tests in this post.
The second option is to use a WebTestRequestPlugIn. Here is a sample code that will help you understand how to stop a web test upon a validation error:
public class StopTestPlugin : WebTestRequestPlugin
{
public override void PostRequest(object sender, PostRequestEventArgs e)
{
}
void Request_ValidateResponse(object sender, ValidationEventArgs e)
{
ValidationRuleFindText ft = new ValidationRuleFindText();
ft.FindText = "The test to find";
ft.IgnoreCase = true;
ft.PassIfTextFound = true;
ft.Validate(sender, e);
if (!e.IsValid)
{
e.WebTest.Stop();
}
}
public override void PreRequest(object sender, PreRequestEventArgs e)
{
e.Request.ValidateResponse += new EventHandler<ValidationEventArgs>(Request_ValidateResponse);
}
}
You can take this code snippet and extend it for your own needs. Enjoy!
תגים:Tests, Team System, .NET, Web Test