Extending Visual Studio 2010 Web Test–Regex extraction

In a previous post I showed how to create a custom loop that permits you to create a loop in a web performance test to iterate from the char ‘a’ to char ‘z’, now I want to be able to create an inner loop that

  1. for each loop extract all the names of the customers that satisfy the search
  2. for each name ask for detail

I need another custom loop that is able to extract strings from the body of a response and iterate for each string. Here is the full code.

   1: public class RegexLoop : ConditionalRule

   2: {

   3:     private Int32 CurrentMatch { get; set; }

   4:     private String LastUrl { get; set; }

   5:     private MatchCollection Matches { get; set; }


   7:     // Methods

   8:     public override void CheckCondition(object sender, ConditionalEventArgs e)

   9:     {


  11:         if (CurrentMatch < Matches.Count)

  12:         {

  13:             e.WebTest.Context[ContextParameterName] =

  14:                 Matches[CurrentMatch].Groups[GroupName].Value;

  15:             e.IsMet = true;

  16:             CurrentMatch++;

  17:             return;


  19:         }


  21:         e.IsMet = false;


  23:     }


  25:     public override void Initialize(object sender, ConditionalEventArgs e)

  26:     {

  27:         CurrentMatch = 0;

  28:         Matches = System.Text.RegularExpressions.Regex.Matches(

  29:             e.WebTest.LastResponse.BodyString,

  30:             Regex,

  31:             RegexOptions.IgnoreCase);

  32:     }


  34:     public override string StringRepresentation()

  35:     {

  36:         return "Regex condition " + Regex;

  37:     }


  39:     [Description("Name of context parameter where the current value should be set"),

  40:     DisplayName("Context parameter"), IsContextParameterName(true)]

  41:     public string ContextParameterName { get; set; }


  43:     [Description("Regex"),

  44:     DisplayName("Regex")]

  45:     public String Regex { get; set; }


  47:     [DisplayName("GroupName"), Description("GroupName")]

  48:     public String GroupName { get; set; }



  51: }

Listing 1: The full code of the RegexLoop plugin class

This plug-in has two property, the first Regex is used to define the regex to use, the other one is the name of Contextparameter used to store the result of the regex. This plugin simply iterates to each match of the regex, and permits you to create a loop based on text of the last request. This plugin is really simple, during initialization I execute the regex against the last response to grab a MatchCollection

   1: public override void Initialize(object sender, ConditionalEventArgs e)

   2: {

   3:   CurrentMatch = 0;

   4:   Matches = System.Text.RegularExpressions.Regex.Matches(

   5:       e.WebTest.LastResponse.BodyString,

   6:       Regex,

   7:       RegexOptions.IgnoreCase);

   8: }

Listing 2: The initialization function

Thanks to the e.WebTest.LastReponse.BodyString I’m able to access the body of the last response and execute the regex against it. The main loop is now straightforward to write

   1: public override void CheckCondition(object sender, ConditionalEventArgs e)

   2: {


   4:     if (CurrentMatch < Matches.Count)

   5:     {

   6:         e.WebTest.Context[ContextParameterName] =

   7:             Matches[CurrentMatch].Groups[GroupName].Value;

   8:         e.IsMet = true;

   9:         CurrentMatch++;

  10:         return;


  12:     }


  14:     e.IsMet = false;

  15: }

Listing 3: The main loop, iterate for each match

Now I can modify the test presented in the old post to insert an inner loop that is able to interate to all customers that satisfy the filter for each request.


Figure 1: Insert a loop based on a regular expression thanks to RegexLoop plugin

I inserted another loop inside the first one, after the search request, and insert a regular expression that is able to find all “select” button of the gridview, I asked the regexloop to insert the value in the CurrentCustomer context variable to use in the subsequent request


Fgirue 2:  Use the CurrentCustomer context variable in the inner loop request

Now you can execute the test and verify the outcome, I’m expecting an external loop to search for all alphabet letters, then for each response an inner loop for each customer, as visible in Figure 3.


Figure 3: The result of the test, you can verify that all loops behave correctly.

Thanks to few lines of code now I’m able to execute loops based on content of a web response, that can be used to create complex web performance test in Visual Studio 2010.


Published by

Ricci Gian Maria

.Net programmer, User group and community enthusiast, programmer - aspiring architect - and guitar player :). Visual Studio ALM MVP

2 thoughts on “Extending Visual Studio 2010 Web Test–Regex extraction”

Leave a Reply

Your email address will not be published.