共用方式為


HOW TO:建立 Web 效能測試的自訂驗證規則

您可以建立自己的驗證規則。 若要執行這項作業,您可以從驗證規則類別中衍生自己的規則類別。 驗證規則衍生自 ValidationRule 基底類別。

Visual Studio Ultimate 提供了一些預先定義的驗證規則。 如需詳細資訊,請參閱在 Web 效能測試中使用驗證規則和擷取規則

注意事項注意事項

您也可以建立自訂擷取規則。如需詳細資訊,請參閱建立和使用負載和 Web 效能測試的自訂外掛程式

需求

  • Visual Studio Ultimate

若要建立自訂驗證規則

  1. 開啟包含 Web 效能測試的測試專案。

  2. (選擇性) 建立個別的類別庫專案,以存放驗證規則。

    重要

    您可以在測試所在的同一個專案中建立類別。不過,如果要重複使用規則,最好是建立個別的類別庫專案,以存放規則。如果您要建立個別的專案,必須完成本程序中的選擇性步驟。

  3. (選擇性) 在「類別庫」專案中,加入 Microsoft.VisualStudio.QualityTools.WebTestFramework DLL 的參考。

  4. 建立從 ValidationRule 類別衍生的類別。 實作 ValidateRuleName 成員。

  5. (選擇性) 建立新的「類別庫」專案。

  6. (選擇性) 在測試專案中,加入包含自訂驗證規則之類別庫專案的參考。

  7. 在 [測試專案] 中,使用 [Web 效能測試編輯器] 開啟 Web 效能測試。

  8. 若要將自訂驗證規則加入至 Web 效能測試要求,請以滑鼠右鍵按一下要求,然後選取 [加入驗證規則]。

    [加入驗證規則] 對話方塊隨即出現。 您會在 [選取規則] 清單中看到您的自訂驗證規則,以及預先定義的驗證規則。 選取您的自訂驗證規則,然後按一下 [確定]。

  9. 執行您的 Web 效能測試。

範例

下列程式碼顯示自訂驗證規則的實作。 這個驗證規則會模擬預先定義之「必要標記」驗證規則的行為。 可以使用這個範例做為您自訂驗證規則的起點。

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

namespace SampleWebTestRules
{
    //-------------------------------------------------------------------------
    // This class creates a custom validation rule named "Custom Validate Tag"
    // The custom validation rule is used to check that an HTML tag with a 
    // particular name is found one or more times in the HTML response.
    // The user of the rule can specify the HTML tag to look for, and the 
    // number of times that it must appear in the response.
    //-------------------------------------------------------------------------
    public class CustomValidateTag : ValidationRule
    {
        /// Specify a name for use in the user interface.
        /// The user sees this name in the Add Validation dialog box.
        //---------------------------------------------------------------------
        public override string RuleName
        {
            get { return "Custom Validate Tag"; }
        }

        /// Specify a description for use in the user interface.
        /// The user sees this description in the Add Validation dialog box.
        //---------------------------------------------------------------------
        public override string RuleDescription
        {
            get { return "Validates that the specified tag exists on the page."; }
        }

        // The name of the required tag
        private string RequiredTagNameValue;
        public string RequiredTagName
        {
            get { return RequiredTagNameValue; }
            set { RequiredTagNameValue = value; }
        }

        // The minimum number of times the tag must appear in the response
        private int MinOccurrencesValue;
        public int MinOccurrences
        {
            get { return MinOccurrencesValue; }
            set { MinOccurrencesValue = value; }
        }

        // Validate is called with the test case Context and the request context.
        // These allow the rule to examine both the request and the response.
        //---------------------------------------------------------------------
        public override void Validate(object sender, ValidationEventArgs e)
        {
            bool validated = false;
            int numTagsFound = 0;

            foreach (HtmlTag tag in e.Response.HtmlDocument.GetFilteredHtmlTags(RequiredTagName))
            {
                Debug.Assert(string.Equals(tag.Name, RequiredTagName, StringComparison.InvariantCultureIgnoreCase));

                if (++numTagsFound >= MinOccurrences)
                {
                    validated = true;
                    break;
                }
            }

            e.IsValid = validated;

            // If the validation fails, set the error text that the user sees
            if (!validated)
            {
                if (numTagsFound > 0)
                {
                    e.Message = String.Format("Only found {0} occurences of the tag", numTagsFound);
                }
                else
                {
                    e.Message = String.Format("Did not find any occurences of tag '{0}'", RequiredTagName);
                }
            }
        }
    }
}
Imports System
Imports System.Diagnostics
Imports System.Globalization
Imports Microsoft.VisualStudio.TestTools.WebTesting

Namespace SampleWebTestRules

    '-------------------------------------------------------------------------
    ' This class creates a custom validation rule named "Custom Validate Tag"
    ' The custom validation rule is used to check that an HTML tag with a 
    ' particular name is found one or more times in the HTML response.
    ' The user of the rule can specify the HTML tag to look for, and the 
    ' number of times that it must appear in the response.
    '-------------------------------------------------------------------------
    Public Class CustomValidateTag
        Inherits Microsoft.VisualStudio.TestTools.WebTesting.ValidationRule

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

        ' Specify a description for use in the user interface.
        ' The user sees this description in the Add Validation dialog box.
        '---------------------------------------------------------------------
        Public Overrides ReadOnly Property RuleDescription() As String
            Get
                Return "Validates that the specified tag exists on the page."
            End Get
        End Property

        ' The name of the required tag
        Private RequiredTagNameValue As String
        Public Property RequiredTagName() As String
            Get
                Return RequiredTagNameValue
            End Get
            Set(ByVal value As String)
                RequiredTagNameValue = value
            End Set
        End Property

        ' The minimum number of times the tag must appear in the response
        Private MinOccurrencesValue As Integer
        Public Property MinOccurrences() As Integer
            Get
                Return MinOccurrencesValue
            End Get
            Set(ByVal value As Integer)
                MinOccurrencesValue = value
            End Set
        End Property

        ' Validate is called with the test case Context and the request context.
        ' These allow the rule to examine both the request and the response.
        '---------------------------------------------------------------------
        Public Overrides Sub Validate(ByVal sender As Object, ByVal e As ValidationEventArgs)

            Dim validated As Boolean = False
            Dim numTagsFound As Integer = 0

            For Each tag As HtmlTag In e.Response.HtmlDocument.GetFilteredHtmlTags(RequiredTagName)

                Debug.Assert(String.Equals(tag.Name, RequiredTagName, StringComparison.InvariantCultureIgnoreCase))

                numTagsFound += 1
                If numTagsFound >= MinOccurrences Then

                    validated = True
                    Exit For
                End If
            Next

            e.IsValid = validated

            ' If the validation fails, set the error text that the user sees
            If Not (validated) Then
                If numTagsFound > 0 Then
                    e.Message = String.Format("Only found {0} occurences of the tag", numTagsFound)
                Else
                    e.Message = String.Format("Did not find any occurences of tag '{0}'", RequiredTagName)
                End If
            End If
        End Sub
    End Class
End Namespace

請參閱

工作

HOW TO:將驗證規則加入至 Web 效能測試

逐步解說:將驗證和擷取規則加入至 Web 效能測試

HOW TO:建立 Web 效能測試的自訂擷取規則

參考

ValidationRule

Microsoft.VisualStudio.TestTools.WebTesting.Rules

ValidateFormField

ValidationRuleFindText

ValidationRuleRequestTime

ValidationRuleRequiredAttributeValue

ValidationRuleRequiredTag

概念

在 Web 效能測試中使用驗證規則和擷取規則