Custom rules

While generating the code for the recorded web test, Visual Studio creates the code for the rules that we added. However, if more custom rules are to be added to the web test, use the Microsoft.VisualStudio.TestTools.WebTesting namespace and create a new rule class, which is derived from the base class. This new class can be a part of the managed class library (which can be a plugin). This can be an extraction rule or a validation rule.

Extraction rules

Extraction rules are used for extracting data from the responses received for web requests. Data can be extracted from text fields, headers, form fields, attributes, or from 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 Extract method and build the custom rule as per requirements. For example, the following screenshot of a code block shows a CustomExtractionRule for extracting the Parameter value from the request:

Extraction rules

Define a property to specify the name of the Parameter value to be extracted.

The extract method is used to extract the data. This method contains two parameters: object and ExtractionEventArgs. The ExtractionEventArgs parameter has the property response, which provides the response generated by the request. This response contains the query string, 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 request and extract the value if a match is found. The method returns a success or failure status along with the message. The extracted value can be added as the context variable using the following code:

e.WebTest.Context.Add(this.ContextParameterName, parameter.Value);

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

The ExtractEventArgs object also contains a return value of either Success or Failure, based on the extraction of the value. The following code block shows the sample of an extraction rule that extracts the value of an input field with a given name:

publicoverridevoid Extract(object sender, ExtractionEventArgs e)
        {
if (e.Response.HtmlDocument != null)
            {
foreach (HtmlTag tag ine.Response.HtmlDocument.GetFilteredHtmlTags(newstring[] { 'input' }))
                {
if (String.Equals(tag.GetAttributeValueAsString('name'), Name, StringComparison.InvariantCultureIgnoreCase))
                    {
stringformFieldValue = tag.GetAttributeValueAsString('value'),
if (formFieldValue == null)
                        {
formFieldValue = String.Empty;
                        }
// add the extracted value to the web performance test context
e.WebTest.Context.Add(this.ContextParameterName, formFieldValue);
e.Success = true;
return;
                    }
                }
            }
// If the extraction fails, set the error text that the user sees
e.Success = false;
            e.Message = String.Format(CultureInfo.CurrentCulture, 'Parameter not Found: {0}', Name);
        }

With the code for the new extraction rule, added recompile the class library. Add the class library reference to the web Test Project and include the namespace to the web test code to make use of the new custom rule. Now, to create a new rule for the request in the web test code, create a new instance of CustomExtractionRule (the class that is created for the custom rule) and set the properties. The following screenshot contains the sample for adding a new rule to the test to extract the First Name value from the edit page and assign that to the param1 parameter:

Extraction rules

When the coded web test is run, the Test Result window shows the extracted value for the parameter, based on the success or failure of the execution rule during the test. The following screenshots show the execution rule result:

Extraction rules

The following screenshot shows the parameter value retrieved during the same Test Run:

Extraction rules

The same custom rule can also be used in the recoded web test. To add the custom rule, open the WebTest project and add a reference to the custom rule project.

Open the web test 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 displays all the types of extraction rules including the custom rule created:

Extraction rules

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

Validation rules

CustomValidationRule is very similar to the extraction rules. It is the custom code derived from the ValidationRule base class. This class is present in the namespace Microsoft.VisualStudio.Testtools.WebTesting. The new custom validation rule can be created as a separate class library, which can be added to the web Test Project when required.

The validation rule is to check if a particular value is found once or more in the HTML responses. The response contains the attributes, parameters, hidden values, in fact the entire response information in the HTML form.

The validation rule has properties and methods similar to the ones in Validate.

Validation rules

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 the string value can be found and the response validated.

The RuleName and RuleDescription properties have become obsolete in this version of Visual Studio but the DisplayName and Description attributes can be used on the class to set the display name and description for the rule.

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

The sample custom validation rule here uses a string to find the value from the response during the Test Run.

Now, compile the custom rules library and add the reference to the project—similar to the extraction rule—and include the required namespace. In the web test code, create a new instance of this custom rule and set the properties. As per the following code, the validation rule is added to find the string value Employee.

Validation rules

Once the test is run, the rule gets executed and the result is added to the output. The Test Result window shows the output of the validation rule, as shown here:

Validation rules

The expected string is not found in the request and the validation rule failed. Hence, the test is also failed. The expected failure message is also shown in the result window.

The custom rule can also be used in the recorded web test. To add the custom rule library created previously, select the recorded web Test Project and add a reference to the library. Open the web test and select the validation rules folder, right-click and select the option Insert Validation Rule, which opens the dialog listing all types of validation rules as shown in the following screenshot:

Validation rules

Now, set the value of the StringValueToFind parameter to something, say Test. Run the web test again and check the results. The result is based on the success or failure of the validation rule.

You can have as many custom rules as required and they can be re-used across multiple tests as well.

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

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