Share via


How to: Create a Custom Extraction Rule for a Web Performance Test

You can create your own extraction rules. To do this, you derive your own rules from an extraction rule class. Extraction rules derive from the ExtractionRule base class.

Visual Studio Ultimate provides some predefined extraction rules. For more information, see Using Validation and Extraction Rules in Web Performance Tests.

Note

You can also create custom validation rules. For more information, see Creating and Using Custom Plug-ins for Load and Web Performance Tests.

Requirements

  • Visual Studio Ultimate

To create a custom extraction rule

  1. Open a Test Project that contains a Web performance test.

  2. (Optional) Create a separate Class library project in which to store your extraction rule.

    Important

    You can create the class in the same project that your tests are in. However, if you want to reuse the rule, it is better to create a separate Class library project in which to store your rule. If you create a separate project, you must complete the optional steps in this procedure.

  3. (Optional) In the Class library project, add a reference to the Microsoft.VisualStudio.QualityTools.WebTestFramework dll.

  4. Create a class that derives from the ExtractionRule class. Implement the Extract and RuleName members.

  5. (Optional) Build the new Class library project.

  6. (Optional) In the Test Project, add a reference to the Class library project that contains the custom extraction rule.

  7. In the Test Project, open a Web performance test in the Web Performance Test Editor.

  8. To add the custom extraction rule, right-click a Web performance test request and select Add Extraction Rule.

    The Add Extraction Rule dialog box appears. You will see your custom validation rule in the Select a rule list, together with the predefined validation rules. Select your custom extraction rule and then choose OK.

  9. Run your Web performance test.

Example

The following code shows an implementation of a custom extraction rule. This extraction rule extracts the value from a specified input field. Use this example as a starting point for your own custom extraction rules.

using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.WebTesting;
using System.Globalization;

namespace ClassLibrary2
{
    //-------------------------------------------------------------------------
    // This class creates a custom extraction rule named "Custom Extract Input"
    // The user of the rule specifies the name of an input field, and the
    // rule attempts to extract the value of that input field.
    //-------------------------------------------------------------------------
    public class CustomExtractInput : ExtractionRule
    {
        /// Specify a name for use in the user interface.
        /// The user sees this name in the Add Extraction dialog box.
        //---------------------------------------------------------------------
        public override string RuleName
        {
            get { return "Custom Extract Input"; }
        }

        /// Specify a description for use in the user interface.
        /// The user sees this description in the Add Extraction dialog box.
        //---------------------------------------------------------------------
        public override string RuleDescription
        {
            get { return "Extracts the value from a specified input field"; }
        }

        // The name of the desired input field
        private string NameValue;
        public string Name
        {
            get { return NameValue; }
            set { NameValue = value; }
        }

        // The Extract method.  The parameter e contains the web performance test context.
        //---------------------------------------------------------------------
        public override void Extract(object sender, ExtractionEventArgs e)
        {
            if (e.Response.HtmlDocument != null)
            {
                foreach (HtmlTag tag in e.Response.HtmlDocument.GetFilteredHtmlTags(new string[] { "input" }))
                {
                    if (String.Equals(tag.GetAttributeValueAsString("name"), Name, StringComparison.InvariantCultureIgnoreCase))
                    {
                        string formFieldValue = 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, "Not Found: {0}", Name);
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports Microsoft.VisualStudio.TestTools.WebTesting
Imports System.Globalization

Namespace ClassLibrary2

    '-------------------------------------------------------------------------
    ' This class creates a custom extraction rule named "Custom Extract Input"
    ' The user of the rule specifies the name of an input field, and the
    ' rule attempts to extract the value of that input field.
    '-------------------------------------------------------------------------
    Public Class CustomExtractInput
        Inherits ExtractionRule

        ' Specify a name for use in the user interface.
        ' The user sees this name in the Add Extraction dialog box.
        '---------------------------------------------------------------------
        Public Overrides ReadOnly Property RuleName() As String
            Get
                Return "Custom Extract Input"
            End Get
        End Property

        ' Specify a description for use in the user interface.
        ' The user sees this description in the Add Extraction dialog box.
        '---------------------------------------------------------------------
        Public Overrides ReadOnly Property RuleDescription() As String
            Get
                Return "Extracts the value from a specified input field"
            End Get
        End Property

        ' The name of the desired input field
        Private NameValue As String
        Public Property Name() As String
            Get
                Return NameValue
            End Get
            Set(ByVal value As String)
                NameValue = value
            End Set
        End Property

        ' The Extract method.  The parameter e contains the web performance test context.
        '---------------------------------------------------------------------
        Public Overrides Sub Extract(ByVal sender As Object, ByVal e As ExtractionEventArgs)

            If Not e.Response.HtmlDocument Is Nothing Then

                For Each tag As HtmlTag In e.Response.HtmlDocument.GetFilteredHtmlTags(New String() {"input"})

                    If String.Equals(tag.GetAttributeValueAsString("name"), Name, StringComparison.InvariantCultureIgnoreCase) Then

                        Dim formFieldValue As String = tag.GetAttributeValueAsString("value")
                        If formFieldValue Is Nothing Then

                            formFieldValue = String.Empty
                        End If

                        ' add the extracted value to the web performance test context
                        e.WebTest.Context.Add(Me.ContextParameterName, formFieldValue)
                        e.Success = True
                        Return
                    End If
                Next
            End If
            ' If the extraction fails, set the error text that the user sees
            e.Success = False
            e.Message = String.Format(CultureInfo.CurrentCulture, "Not Found: {0}", Name)
        End Sub
    End Class
end namespace

The Extract method contains the core functionality of an extraction rule. The Extract method in the previous example takes an ExtractionEventArgs that provides the response generated by the request this extraction rule covers. The response contains an HtmlDocument which contains all the tags in the response. Input tags are filtered out of the HtmlDocument. Each input tag is examined for an attribute called name whose value equals the user supplied value of the Name property. If a tag with this matching attribute is found, an attempt is made to extract a value that is contained by the value attribute, if a value attribute exists. If it exists, the name and value of the tag are extracted and added to the web performance test context. The extraction rule passes.

See Also

Tasks

How to: Add an Extraction Rule to a Web Performance Test

Walkthrough: Adding Validation and Extraction Rules to a Web Performance Test

How to: Create a Custom Validation Rule for a Web Performance Test

Reference

ExtractionRule

Microsoft.VisualStudio.TestTools.WebTesting.Rules

ExtractAttributeValue

ExtractFormField

ExtractHttpHeader

ExtractRegularExpression

ExtractText

ExtractHiddenFields

Concepts

Using Validation and Extraction Rules in Web Performance Tests