如何:创建自定义提取规则

更新:2007 年 11 月

可以创建自己的提取规则。若要执行此操作,需从提取规则类派生出自己的规则。从 ExtractionRule 基类派生提取规则。

Visual Studio Team System Test Edition 提供了一些预定义的提取规则。有关更多信息,请参见关于提取规则

说明:

还可以创建自定义验证规则。有关更多信息,请参见关于验证规则

创建自定义提取规则

  1. 打开包含 Web 测试的测试项目。

  2. (可选)创建一个单独的类库项目来存储您的提取规则。

    重要说明:

    您可以在测试所在的项目中创建类。但是,如果您希望重用规则,则最好创建一个单独的类库项目来存储规则。如果创建单独的项目,则必须完成本过程中的可选步骤。

  3. (可选)在该类库项目中,添加一个对 Microsoft.VisualStudio.QualityTools.WebTestFramework dll 的引用。

  4. 创建一个从 ExtractionRule 类派生的类。实现 ExtractRuleName 成员。

  5. (可选)生成新的类库项目。

  6. (可选)在测试项目中添加一个对包含自定义提取规则的类库项目的引用。

  7. 在测试项目中,在“Web 测试编辑器”中打开一个 Web 测试。

  8. 若要添加自定义提取规则,请右击某 Web 测试请求并选择“添加提取规则”。

    将出现“添加提取规则”对话框。您将在“选择规则”列表中看到您的自定义验证规则以及预定义的验证规则。选择自定义提取规则,然后单击“确定”。

  9. 运行 Web 测试。

示例

下面的代码演示如何实现自定义提取规则。该提取规则从指定的输入字段中提取值。基于该示例来创建您自己的自定义提取规则。

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;
                        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 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 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

Extract 方法中包含了提取规则的核心功能。前面示例中的 Extract 方法接受 ExtractionEventArgs 参数,该参数用于提供此提取规则涵盖范围内的请求生成的响应。该响应包含一个 HtmlDocument,其中包含了响应中的所有标记。输入标记会从 HtmlDocument 筛选排除掉。会对每个输入标记的 name 属性进行检查,以确定其值是否与用户所提供的 Name 属性的值相等。如果找到具有此匹配属性的标记,便会尝试提取 value 属性所包含的值(如果存在 value 属性的话)。如果该值存在,则会提取标记的名称和值,并将它们添加到 Web 测试上下文中。提取规则通过。

请参见

任务

如何:向 Web 测试添加提取规则

演练:向 Web 测试添加验证和提取规则

如何:创建自定义验证规则

参考

ExtractionRule

Microsoft.VisualStudio.TestTools.WebTesting.Rules

ExtractAttributeValue

ExtractFormField

ExtractHttpHeader

ExtractRegularExpression

ExtractText

ExtractHiddenFields