Share via


ExtractionRule Class

Base class used to define rules for obtaining data from a Web response that is generated by a Web performance test.

Inheritance Hierarchy

System.Object
  Microsoft.VisualStudio.TestTools.WebTesting.ExtractionRule
    Microsoft.VisualStudio.TestTools.WebTesting.ExtractHtmlSelectTag
    Microsoft.VisualStudio.TestTools.WebTesting.ExtractHtmlTagInnerText
    Microsoft.VisualStudio.TestTools.WebTesting.Rules.ExtractAttributeValue
    Microsoft.VisualStudio.TestTools.WebTesting.Rules.ExtractFormField
    Microsoft.VisualStudio.TestTools.WebTesting.Rules.ExtractHiddenFields
    Microsoft.VisualStudio.TestTools.WebTesting.Rules.ExtractHttpHeader
    Microsoft.VisualStudio.TestTools.WebTesting.Rules.ExtractRegularExpression
    Microsoft.VisualStudio.TestTools.WebTesting.Rules.ExtractText

Namespace:  Microsoft.VisualStudio.TestTools.WebTesting
Assembly:  Microsoft.VisualStudio.QualityTools.WebTestFramework (in Microsoft.VisualStudio.QualityTools.WebTestFramework.dll)

Syntax

'Declaration
Public MustInherit Class ExtractionRule
public abstract class ExtractionRule
public ref class ExtractionRule abstract
[<AbstractClass>]
type ExtractionRule =  class end
public abstract class ExtractionRule

The ExtractionRule type exposes the following members.

Constructors

  Name Description
Protected method ExtractionRule Initializes a new instance of the ExtractionRule class.

Top

Properties

  Name Description
Public property ContextParameterName Gets or sets the context name of the extracted property.
Public property RuleDescription Obsolete. This method is no longer used. Use the DisplayNameAttribute on the class to set a description for this rule.
Public property RuleName Obsolete. This method is no longer used. Use the DisplayNameAttribute on the class to set a display name for this rule.

Top

Methods

  Name Description
Public method Equals Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Public method Extract When overridden in a derived class, this method extracts information from a HtmlDocument and places it into the WebTestContext.
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

The ExtractionRule class must be inherited by any extraction rule whether it is user-written or built-in. Extraction rules that are associated with the request definition are run after a response has been received, and the results of the extraction are added to the WebTestContext.

Notes to Inheritors

When you inherit from ExtractionRule, you must override the following members: Extract, RuleDescription, and RuleName.

Examples

The following custom extraction rule extracts check boxes from the HtmlDocument and places the status of the found check boxes into the context.

using System;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace ClassLibrary2
{
    public class ExtractCheckBoxes : ExtractionRule
    {
        private string myContextParameter = "ExtractCBoxParam";
        private bool isChecked = true;

        public override string ContextParameterName
        {
            get { return myContextParameter; }
            set
            {
                if (value != string.Empty)
                    myContextParameter = value;
            }
        }

        public bool FindCheckedBoxes
        {
            get { return isChecked; }
            set { isChecked = value; }
        }

        public override string RuleName
        {
            get { return "Extract Check Boxes"; }
        }

        public override string RuleDescription
        {
            get { return "Extract check boxes based on whether they are" +
                    " checked or not."; }
        }

        public override void Extract(object sender, ExtractionEventArgs e)
        {   
            e.Success = false;
            
            if (e.Response.HtmlDocument != null)
            {   // Gets all input tags
                foreach (HtmlTag tag in e.Response.HtmlDocument
                    .GetFilteredHtmlTags(new string[] { "input" }))
                {   // Verify that current tag is a checkbox
                    if (tag.GetAttributeValueAsString("type") == "checkbox")
                    {   // Is the checkbox checked
                        if (tag.GetAttributeValueAsString("checked") == "CHECKED")
                        {   // Add checked check boxes to context
                            if (isChecked)
                            {
                                e.WebTest.Context.Add(myContextParameter + "_" +
                                    tag.GetAttributeValueAsString("id"),
                                    "Is Checked");
                                e.Success = true;
                            }
                        }
                        else // The checkbox is not checked
                        {   // Add non-checked boxes to context
                            if (!isChecked)
                            {
                                e.WebTest.Context.Add(myContextParameter + "_" +
                                    tag.GetAttributeValueAsString("id"),
                                    "Is Not Checked");
                                e.Success = true;
                            }
                        }
                    }
                }
            }
            if (e.Success)
                e.Message = "Extracted check boxes.";
            else
                e.Message = "No check boxes extracted.";
        }
    }
}
Imports System
Imports Microsoft.VisualStudio.TestTools.WebTesting

Namespace ClassLibrary2
    Public Class ExtractCheckBoxes
        Inherits ExtractionRule

        Private myContextParameter As String = "ExtractCBoxParam"
        Private isChecked As Boolean = True

        Public Overrides Property ContextParameterName() As String
            Get
                Return myContextParameter
            End Get
            Set(ByVal value As String)
                If (value <> String.Empty) Then
                    myContextParameter = value
                End If
            End Set
        End Property

        Public Property FindCheckedBoxes() As Boolean
            Get
                Return isChecked
            End Get
            Set(ByVal value As Boolean)
                isChecked = value
            End Set
        End Property

        Public Overrides ReadOnly Property RuleName() As String
            Get
                Return "Extract Check Boxes"
            End Get
        End Property

        Public Overrides ReadOnly Property RuleDescription() As String
            Get
                Return "Extract check boxes based on whether they are" + _
                    " checked or not."
            End Get
        End Property

        Public Overrides Sub Extract(ByVal sender As Object, ByVal e As ExtractionEventArgs)
            e.Success = False
            If Not e.Response.HtmlDocument Is Nothing Then
                ' Gets all input tags
                Dim tag As HtmlTag
                For Each tag In e.Response.HtmlDocument.GetFilteredHtmlTags(New String() {"input"})
                    ' Verify if current tag is a checkbox
                    If tag.GetAttributeValueAsString("type") = "checkbox" Then
                        ' Is the check box checked
                        If tag.GetAttributeValueAsString("checked") = "CHECKED" Then
                            ' Add checked checkbox to context
                            If isChecked = True Then
                                e.WebTest.Context.Add(myContextParameter + "_" + _
                                tag.GetAttributeValueAsString("id"), "Is Checked")
                                e.Success = True
                            End If
                        Else ' The check box is not checked
                            If isChecked = False Then
                                ' Add non-checked boxes to context
                                e.WebTest.Context.Add(myContextParameter + "_" + _
                                tag.GetAttributeValueAsString("id"), "Is Not Checked")
                                e.Success = True
                            End If
                        End If
                    End If
                Next
            End If
            If e.Success = True Then
                e.Message = "Extracted check boxes."
            Else
                e.Message = "No check boxes extracted."
            End If
        End Sub
    End Class
End Namespace

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

Microsoft.VisualStudio.TestTools.WebTesting Namespace

Other Resources

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

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