Writing Custom Conditional Rule for Visual Studio 2010

Problem:

I have a portion of a web test I only want to run if a specific condition is true.

Solution:

Use the new Visual Studio 2010 Conditional Rule Feature.  However, the condition I wanted to test for was unable to be tested using the out of the box conditions. So in this posting I am going to show you how to write your own custom Condition Rule for Visual Studio 2010.

Code:

using System;
using System.ComponentModel;
using System.Text.RegularExpressions; 

namespace DLB
{
   [DisplayName("Find Text")]
   [Description("Searches the last response for the existence of the specified text.")]
   public class FindTextConditionalRule : Microsoft.VisualStudio.TestTools.WebTesting.ConditionalRule
   {
      [DisplayName("Find Text")]
      [Description("The text to search for in the last response of the web test.")]
      public string Value { get; set; } 

      [DisplayName("Ignore Case")]
      [Description("If true case is ignored while seaching for the string in the response.")]
      [DefaultValue(true)]
      public bool IgnoreCase { get; set; } 

      [DisplayName("Use Regular Expression")]
      [Description("Use a regular expression during search.")]
      public bool UseRegularExpression { get; set; } 

      [DisplayName("Pass If Text Found")]
      [Description("Condition passes if the text is found when this property is set true, or when the text is not found and this property is set false.")]
      [DefaultValue(true)]
      public bool PassIfNotFound { get; set; } 

      /// <summary>
      /// Determines whether the condition was met or not.
      /// </summary>
      /// <param name="sender">The sender of the event.</param>
      /// <param name="e">The Microsoft.VisualStudio.TestTools.WebTesting.ConditionalEventArgs that contains the event data.</param>
      public override void CheckCondition(object sender, Microsoft.VisualStudio.TestTools.WebTesting.ConditionalEventArgs e)
      {
         if(UseRegularExpression)
         {
            Match result = Regex.Match(e.WebTest.LastResponse.BodyString, Value, IgnoreCase ? RegexOptions.IgnoreCase : RegexOptions.None);
            e.IsMet = result.Success;
         }
         else
         {
            int index = e.WebTest.LastResponse.BodyString.IndexOf(Value, IgnoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture);
            e.IsMet = index == -1 ? false : true;
         } 

         if(PassIfNotFound)
            e.IsMet = !e.IsMet;
      }
   }
}

Explanation:

 

To create a custom Conditional Rule simply create a public class that derives from Microsoft.VisualStudio.TestTools.WebTesting.ConditionalRule and override CheckCondition. This class can be added directly to your test project or a separate class library referenced by your test project.

From within the CheckCondition method set e.IsMet to true or false depending on if the condition was met. If e.IsMet is set to true all the request under this condition will be executed, otherwise, they will be skipped.

Once your test project has a reference to the conditional rule class it will be shown in the Add Conditional Rule and Items to Condition dialog.

 

Once the condition is added to your web test you will see the If statement with the selected requested nested underneath.

 

The sample code is modeled after the Find Text Validation rule and exposes many of the same properties. The purpose of this condition is to search the last response of the web test for the existence or nonexistence of the specified text. If the condition is met the requests under this condition will execute, otherwise the requests will be skipped.

You can download the sample file from here.

FindTextConditionalRule.cs (2.11 kb)

Comments (3) -

  • Malli

    8/3/2017 8:20:45 AM | Reply

    Thanks for the solution, Could you please let me know how to create a class in VSTS to use the code

    • Osccar

      11/14/2017 7:44:00 PM | Reply

      To create a class just right click on the upper level of your class desired location. For instance a folder. Select Add -> Class is at the bottom and goes from there.

  • Sanjiv

    9/2/2020 8:53:16 PM | Reply

    I have these Possible 21 values that may appear in https response: 'Online', 'Restoring', 'RecoveryPending', 'Recovering', 'Suspect', 'Offline', 'Standby', 'Shutdown', 'EmergencyMode', 'AutoClosed', 'Copying', 'Creating', 'Inaccessible', 'OfflineSecondary', 'Pausing', 'Paused', 'Resuming', 'Scaling', 'OfflineChangingDwPerformanceTiers', 'OnlineChangingDwPerformanceTiers', 'Disabled'

    How can I make my web test pass if "Online" or "Scaling" appears in the https response?

    As of today, I am including all the possible values, except "Online" and "Scaling" and setting it to False for "Pass if Text Found". This way my web test is successful.

    I want to avoid adding Find Text multiple times and just look for "Online" and "Scaling" and make the webtest a pass.

Add comment

Loading