Custom rules

When we generate the code for the recorded Web Performance Test, Visual Studio creates the code for the rules that we added for the recorded test. However if we need more custom rules to be added to the Web Performance Test, we can use the Microsoft.VisualStudio.TestTools.WebTesting namespace and create a new rule class which derives from the base class. This new class can be part of the managed class library which can be a plug-in. This can be an extraction rule or a validation rule.

Extraction rule

The extraction rules are used for extracting data from the responses received for the web requests. Data can be extracted from text fields, headers, form fields, attributes, or hidden fields. The new custom extraction rule is a new class file derived from the base class ExtractionRule, which is in the namespace Microsoft.VisualStudio.TestTools.WebTesting. Add a reference to the library, Microsoft.VisualStudio.QualityTools.WebTestFramework, which contains the base classes. In the new class, implement the RuleName, RuleDescripton, and Extract methods and build the custom rule as per requirements.

For example, the following screenshot shows a CustomExtractionRule for extracting the Query String Parameter value from the request:

Extraction rule
  • ParameterName: This is to specify the name of the Query String Parameter that we want.
  • RuleName: This is to specify the name of the new Extraction Rule. The extraction rule dialog will show the input field for specifying the rule name.
    public override string RuleName // to specify the Rule name for this rule {
    get { return "New Custom Extraction Rule";}
    }
    
  • RuleDescription: This is to specify the rule description for the new rule. The Extraction Rule dialog will have input field to specify the description.
    public override string RuleDescription // to specify the description for this rule {
    get {
    return "This is the custom rule for extracting the value of input field at index 1";
    }
    }
    
  • Extract: This is the method to extract the data. This method is applicable only for the Extraction Rule. This method contains two parameters: object and ExtractionEventArgs. The ExtractionEventArgs has the property response, which provides the response generated by the request. The response contains the querystring, attributes, and the HTML documents along with all the other details about the response. Once the test is run, the extraction rule gets executed. In the example shown previously, the extract method will find the specified parameter in the Query String and extract the value if a match is found. The method returns with a success or failure status along with the message. The extracted value can be added as the context variable using the code:
    e.WebTest.Context.Add(this.ContextParameterName, parameter.Value);
    

    The context contains the key value pair, where the key is equal to the ContextParameterName and the value is the parameter value that is extracted.

    // add the extracted value to the Web test context
    e.WebTest.Context.Add(this.ContextParameterName, fieldValue);
    e.Success = true;
    
    

The ExtractEventArgs object also contains a return value of either Success or Failure. We should set this to success or failure based on the extraction of the value. The following code shows the sample of an extraction rule, which extracts the value of an input field with the given name:

public override void Extract(object sender, ExtractionEventArgs e)
{
if (e.Request.HasQueryStringParameters)
{
foreach (QueryStringParameter parameter in e.Request. QueryStringParameters)
{
if (parameter.Name.Equals(Name, StringComparison. CurrentCultureIgnoreCase))
{
if (parameter.Value != null)
{
e.Success = true;
e.Message = String.Format("Parameter Found with value {0}: ", Name);
e.WebTest.Context.Add(this.ContextParameterName, parameter.Value);
}
return;
}
}
e.Success = false;
e.Message = String.Format("Parameter {0} not Found ", ParameterName);
}

After completing the class file with the code for the new extraction rule, compile the class library. Add the class library reference to the Web Performance Test project and include the namespace to the Web Performance Test code to make use of the new custom rule. Now to create a new rule for the request in the Web Performance Test code, create a new instance of the CustomExtractionRule, which is the class that we created for the custom rule, and set the properties. The following code contains the sample for an example of adding a new rule to the test. The extracted value will be added to the list of extracted values for the requests:

Extraction rule

The custom rule can also be used in the recoded Web Performance Test. To add the custom rule, open the WebTest project and add the reference to the custom rule project.

Extraction rule

Now open the Web Performance Test project and select the request for which the new extraction rule should be added. Expand the test recording, select the Extraction Rules folder for the request and select the Add Extraction Rule option which will display all the different types of Extraction rules including the rule we created.

Extraction rule

The CustomExtractionRule is just like the other rule, but is custom-built for the required functionality.

Validation rules

CustomValidationRule is written similar to extraction rules. It is the custom code written as a class, which is derived from the ValidationRule base class. This class is present in the namespace, Microsoft.VisualStudio.Testtools.WebTesting. The new class can be a separate class library, which can be added to the Web Performance Test project when required.

Create a new class library project and a add reference to the Microsoft.VisualSudio.QualityTools.WebTestFramework assembly.

The validation rule is to check if a particular value is found once or more in the HTML response. The response contains the attributes, parameters, hidden values, and the entire HTML document from which we have to find the value to validate.

The validate rule has similar properties and methods as to Rule Name, Rule Description, and Validate.

The Validate method contains two parameters, object and ValidationEventArgs. The ValidationEventArgs object contains the response property that provides the response text for the request through which we can find out the string value and validate the response.

The Validate method should set e.IsValid to true if the validation succeeds, or false if not. The following code is to find a string value in the document. At the same time, e.Message should also be set to a message based on the result, which will be shown at the end of test.

Validation rules

Now compile the library and add the reference to the project similar to the extraction rule and add include the namespace. In the Web Performance Test code, create a new instance of this custom rule and set the properties. Once the test is run, this rule gets executed and the result is added to the requested output.

Validation rules

The custom rule can also be used in the recorded Web Performance Test. To add the custom rule library created previously, select the recorded Web Performance Test project and add the reference to the library. Open the Web Performance Test and select the validation rules folder, right-click and select the option Insert Validation Rule, which opens the dialog, listing out all types of validation rules.

Validation rules

Now set the value of the StringValuetoFind parameter to something. Let us say the value is Test. Run the Web Performance Test again and check the results. The following is example of the test which returned failure and the message along with the result:

Validation rules

The message says: String 'xyz' is not found. We can add as many custom rules like this as we like and validate their responses.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.86.121