Vytvoření vlastního ověřovacího pravidla pro test výkonnosti webu
Můžete vytvořit vlastní pravidla ověření.Chcete-li to provést, odvozujete od třídy pravidlo ověření vlastní pravidlo třídu.Ověřovací pravidla jsou odvozeny od ValidationRule základní třídy.
Visual Studio Ultimateposkytuje několik předdefinovaných ověřovacích pravidel.Další informace naleznete v tématu [vyřazeno] Použití pravidel ověřování a extrakce v testech výkonnosti webu.
[!POZNÁMKA]
Můžete také vytvořit vlastní extrakce pravidla.Další informace naleznete v tématu Vytvoření vlastního kódu a modulů plugin pro zátěžové testování.
Požadavky
- Visual Studio Ultimate
Chcete-li vytvořit vlastní ověřovací pravidla
Otevřete projekt testů obsahující test výkonnosti webu.
(Volitelné) Vytvořte samostatné projekt knihovny tříd pro uložení ověřovací pravidlo.
Důležité
Třídu lze vytvořit ve stejném projektu, ve kterém jsou testy.Nicméně pokud chcete pravidlo znovu použít, je vhodnější vytvořit samostatný projekt knihovny tříd, do které pravidlo uložíte.Pokud vytvoříte samostatný projekt, je nutné provést volitelné kroky v tomto postupu.
(Volitelné) V projektu knihovny tříd přidejte odkaz na knihovnu DLL Microsoft.VisualStudio.QualityTools.WebTestFramework.
Vytvořte třídu, která je odvozena od třídy ValidationRule.Implementujte členy Validate a RuleName.
(Volitelné) Vytvořte nový projekt knihovny tříd.
(Volitelné) V projektu pro Test přidejte odkaz na projekt knihovny tříd, který obsahuje vlastní ověřovacího pravidla.
V projektu testů otevřete test výkonnosti webu v Editoru testů výkonu webu.
Chcete-li přidat vlastní ověřovacího pravidla pro webové žádosti test výkonu, klikněte pravým tlačítkem na požadavek a vyberte přidat pravidlo ověření.
Přidat pravidlo ověření se zobrazí dialogové okno.V seznamu Vybrat pravidlo se společně s předdefinovanými ověřovacími pravidly zobrazí vlastní ověřovací pravidlo.Vyberte svůj vlastní ověřovací pravidlo a pak zvolte OK.
Spusťte test výkonnosti webu.
Příklad
Následující kód ukazuje implementace vlastní ověřovací pravidlo.Toto pravidlo ověření napodobuje chování předdefinované ověřovacího pravidla požadované značky.V tomto příkladu pomocí jako výchozí bod pro vlastní pravidla vlastní ověření.
![]() |
---|
Veřejné vlastnosti v kódu pro vlastní validátor nesmí obsahovat hodnoty null. |
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
Viz také
Úkoly
[vyřazeno] Postupy: Přidání ověřovacího pravidla do testu výkonnosti webu
Vytvoření vlastního pravidla extrakce pro test výkonnosti webu
Referenční dokumentace
Microsoft.VisualStudio.TestTools.WebTesting.Rules
ValidationRuleRequiredAttributeValue
Koncepty
[vyřazeno] Použití pravidel ověřování a extrakce v testech výkonnosti webu
Další zdroje
[vyřazeno] Přidání pravidel ověřování a extrakce k testu výkonnosti webu