Classe ValidationRule
Verifica as solicitações retornam respostas HTTP válidas e que o Sumário da resposta corresponde os resultados esperados.Essa classe deve ser herdada.
Namespace: Microsoft.VisualStudio.TestTools.WebTesting
Assembly: Microsoft.VisualStudio.QualityTools.WebTestFramework (em Microsoft.VisualStudio.QualityTools.WebTestFramework.dll)
Sintaxe
Public MustInherit Class ValidationRule
Dim instance As ValidationRule
public abstract class ValidationRule
public ref class ValidationRule abstract
public abstract class ValidationRule
Comentários
Regras de validação escritos por usuário precisam derivar dessa classe.Regras de validação são executadas apenas depois que a solicitação for concluída.
Observações para Inheritors:
Quando você herdar de ValidationRule, você deve substituir o Validate método e RuleName propriedade.
Exemplos
O exemplo de código a seguir mostra como fazer herança da ValidationRule Para criar uma regra que valida a existência de scripts na página da Web.
using System;
using Microsoft.VisualStudio.TestTools.WebTesting;
namespace MyValidationRule
{
public class ValidatePageContainsScript : ValidationRule
{
public override string RuleName
{
get { return "Validate Script Existence"; }
}
public override string RuleDescription
{
get { return "Validates that the page has a script."; }
}
public override void Validate(object sender, ValidationEventArgs e)
{
bool validated = false;
string foundJS ="";
string foundVBS = "";
string message = "Non-valid HTML document";
if (e.Response.HtmlDocument != null)
{ // Gets all input tags
foreach (HtmlTag tag in e.Response.HtmlDocument
.GetFilteredHtmlTags(new string[] { "script" }))
{ // Check type of script for current tag
if (tag.GetAttributeValueAsString("type") == "text/JavaScript")
foundJS = "Found JavaScript";
if (tag.GetAttributeValueAsString("type") == "text/VBScript")
foundVBS = "Found VBScript";
}
if (foundVBS.Length != 0 || foundJS.Length != 0)
{
validated = true;
message = string.Format("{0} {1}", foundJS, foundVBS);
}
else
{
message = "No scripts in current page";
}
}
e.IsValid = validated;
e.Message = message;
}
}
}
Imports System
Imports Microsoft.VisualStudio.TestTools.WebTesting
Namespace MyValidationRule
Public Class ValidatePageContainsScript
Inherits ValidationRule
Public Overrides ReadOnly Property RuleName() As String
Get
Return "Validate Script Existence"
End Get
End Property
Public Overrides ReadOnly Property RuleDescription() As String
Get
Return "Validates that the page has a script"
End Get
End Property
Public Overrides Sub Validate(ByVal sender As Object, _
ByVal e As ValidationEventArgs)
Dim validated As Boolean = False
Dim foundJS As String = String.Empty
Dim foundVBS As String = String.Empty
Dim message As String = "Non-valid HTML document"
If Not e.Response.HtmlDocument Is Nothing Then
' Get all input tags
Dim tag As HtmlTag
For Each tag In e.Response.HtmlDocument. _
GetFilteredHtmlTags(New String() {"script"})
' Check type of script for current tag
If tag.GetAttributeValueAsString("type") = "text/JavaScript" _
Then
foundJS = "Found JavaScript"
End If
If tag.GetAttributeValueAsString("type") = "text/VBScript" _
Then
foundVBS = "Found VBScript"
End If
Next
If Not foundVBS.Length = 0 Or Not foundJS.Length = 0 Then
validated = True
message = String.Format("{0} {1}", foundJS, foundVBS)
Else
message = "No scripts in current page."
End If
End If
e.IsValid = validated
e.Message = message
End Sub
End Class
End Namespace
Hierarquia de herança
System.Object
Microsoft.VisualStudio.TestTools.WebTesting.ValidationRule
Microsoft.VisualStudio.TestTools.WebTesting.Rules.ValidateFormField
Microsoft.VisualStudio.TestTools.WebTesting.Rules.ValidateResponseUrl
Microsoft.VisualStudio.TestTools.WebTesting.Rules.ValidationRuleFindText
Microsoft.VisualStudio.TestTools.WebTesting.Rules.ValidationRuleRequestTime
Microsoft.VisualStudio.TestTools.WebTesting.Rules.ValidationRuleRequiredAttributeValue
Microsoft.VisualStudio.TestTools.WebTesting.Rules.ValidationRuleRequiredTag
Acesso thread-safe
Quaisquer membros static (Shared no Visual Basic) públicos deste tipo são thread-safe. Não há garantia de que qualquer membro de instância seja thread-safe.
Consulte também
Referência
Namespace Microsoft.VisualStudio.TestTools.WebTesting