Welcome to MSDN Blogs Sign in | Join | Help

Check and modify the status of extraction or validation rules

The following came up on our internal discussion list today.  A user wanted to run an extraction rule and execute some different requests based on whether the rule succeeded or not.  The problem is that a failed extraction rule normally causes the web test to fail.  Fortunately, there's an easy way to check the success status of the rule, use that value later, and prevent the rule from failing the web test.

 

Let's say you have coded web test like the following:

 

        public override IEnumerator<WebTestRequest> GetRequestEnumerator()

        {

            WebTestRequest request1 = new WebTestRequest("http://vsnc/");

            request1.RecordedResponseUrl = "http://vsnc/";

            ExtractText extractionRule1 = new ExtractText();

            extractionRule1.StartsWith = "Logged in as ";

            extractionRule1.EndsWith = ".";

            extractionRule1.IgnoreCase = false;

            extractionRule1.UseRegularExpression = false;

            extractionRule1.Required = true;

            extractionRule1.Index = 0;

            extractionRule1.HtmlDecode = true;

            extractionRule1.ContextParameterName = "Name";

            request1.ExtractValues += new EventHandler<ExtractionEventArgs>(extractionRule1.Extract);

            yield return request1;

        }

 

Instead of hooking up the extraction rule directly to the ExtractValues event, you can prevent the web test from failing by using a custom event handler.  Your event handler can check and even modify values on the ExtractionEventArgs as shown below:

 

public override IEnumerator<WebTestRequest> GetRequestEnumerator()

{

    WebTestRequest request1 = new WebTestRequest("http://vsnc/");

    request1.RecordedResponseUrl = "http://vsnc/";

    request1.ExtractValues += new EventHandler<ExtractionEventArgs>(request1_ExtractValues);

    yield return request1;

 

    if ((bool)this.Context["LogInNameFound"] == true)

    {

        //do something, issue different requests, etc.

    }

}

 

void request1_ExtractValues(object sender, ExtractionEventArgs e)

{

    ExtractText extractionRule1 = new ExtractText();

    extractionRule1.StartsWith = "Logged in as ";

    extractionRule1.EndsWith = ".";

    extractionRule1.IgnoreCase = false;

    extractionRule1.UseRegularExpression = false;

    extractionRule1.Required = true;

    extractionRule1.Index = 0;

    extractionRule1.HtmlDecode = true;

    extractionRule1.ContextParameterName = "Name";

   

    //call the extraction rule directly

    extractionRule1.Extract(sender, e);

 

    //here's where I want to check or modify the success status of the rule

 

    if (e.Success)

    {

        //set a context parameter for use later in the web test

        this.Context["LogInNameFound"] = true;

    }

    else

    {

        //set a context parameter to indicate this rule failed

        this.Context["LogInNameFound"] = false;

 

        //force the rule to pass

        e.Success = true;

    }

}

 

As you can see, inserting your own event handler can give you more control over the execution of extraction and validation rules.

Published Thursday, November 16, 2006 11:01 PM by JoshCh

Comments

# VSTS Links - 11/22/2006

Charles Sterling on Scripting the addition of user accounts to Team Foundation Server. Josh Christie...

Tuesday, November 21, 2006 11:02 PM by Team System News

# Software Testers Deep Dive

We recently ran a bootcamp for some of our internal teams on the testing capabilities of Visual Studio

Friday, March 02, 2007 1:44 PM by Jeff Beehler's Blog

# Content Index for Web Tests and Load Tests

Visual Studio Team System for Testers Content Index for Web Tests and Load Tests Getting Started Online

Wednesday, December 19, 2007 4:12 PM by Ed Glas's blog on VSTS load testing
New Comments to this post are disabled
 
Page view tracker