We recommend using Visual Studio 2017
This documentation is archived and is not being maintained.

How to: Create a Custom Extraction Rule

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 Team Edition for Testers provides some predefined extraction rules. For more information, see About Extraction Rules.


You can also create custom validation rules. For more information, see About Validation Rules.

To create a custom extraction rule

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

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


    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 test in the Web Test Editor.

  8. To add the custom extraction rule, right-click a Web 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 click OK.

  9. Run your Web test.


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 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 test context
                        e.WebTest.Context.Add(this.ContextParameterName, formFieldValue);
                        e.Success = true;
            // 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);

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 test context. The extraction rule passes.

See Also