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.