HOW TO:建立 Web 效能測試的自訂驗證規則
這個主題適用於:
Visual Studio Ultimate |
Visual Studio Premium |
Visual Studio Professional |
Visual Studio Express |
---|---|---|---|
您可以建立自己的驗證規則。 若要執行這項作業,您可以從驗證規則類別中衍生自己的規則類別。 驗證規則衍生自 ValidationRule 基底類別。
Visual Studio Ultimate 提供了一些預先定義的驗證規則。 如需詳細資訊,請參閱在 Web 效能測試中使用驗證規則和擷取規則。
注意事項 |
---|
您也可以建立自訂擷取規則。 如需詳細資訊,請參閱建立和使用負載和 Web 效能測試的自訂外掛程式。 |
若要建立自訂驗證規則
開啟包含 Web 效能測試的測試專案。
(選擇性) 建立個別的類別庫專案,以存放驗證規則。
重要事項 您可以在測試所在的同一個專案中建立類別。 不過,如果要重複使用規則,最好是建立個別的類別庫專案,以存放規則。 如果您要建立個別的專案,必須完成本程序中的選擇性步驟。
(選擇性) 在「類別庫」專案中,加入 Microsoft.VisualStudio.QualityTools.WebTestFramework DLL 的參考。
建立從 ValidationRule 類別衍生的類別。 實作 Validate 和 RuleName 成員。
(選擇性) 建立新的「類別庫」專案。
(選擇性) 在測試專案中,加入包含自訂驗證規則之類別庫專案的參考。
在 [測試專案] 中,使用 [Web 效能測試編輯器] 開啟 Web 效能測試。
若要將自訂驗證規則加入至 Web 效能測試要求,請以滑鼠右鍵按一下要求,然後選取 [加入驗證規則]。
[加入驗證規則] 對話方塊隨即出現。 您會在 [選取規則] 清單中看到您的自訂驗證規則,以及預先定義的驗證規則。 選取您的自訂驗證規則,然後按一下 [確定]。
執行您的 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
請參閱
工作
參考
Microsoft.VisualStudio.TestTools.WebTesting.Rules
ValidationRuleRequiredAttributeValue