ValidationRule 类
验证请求是否返回有效的 HTTP 响应以及响应的内容是否与预期的结果匹配。 必须继承此类。
继承层次结构
System.Object
Microsoft.VisualStudio.TestTools.WebTesting.ValidationRule
更多...
命名空间: Microsoft.VisualStudio.TestTools.WebTesting
程序集: Microsoft.VisualStudio.QualityTools.WebTestFramework(在 Microsoft.VisualStudio.QualityTools.WebTestFramework.dll 中)
语法
声明
Public MustInherit Class ValidationRule
public abstract class ValidationRule
public ref class ValidationRule abstract
[<AbstractClass>]
type ValidationRule = class end
public abstract class ValidationRule
ValidationRule 类型公开以下成员。
构造函数
名称 | 说明 | |
---|---|---|
ValidationRule | 必须继承此类。无法实例化。 |
页首
属性
名称 | 说明 | |
---|---|---|
RuleDescription | 已过时。获取当选择规则时在用户界面中显示的说明。 | |
RuleName | 已过时。当在派生类中重写时,获取当规则选定或显示在编辑器中时用户界面中显示的名称。 |
页首
方法
名称 | 说明 | |
---|---|---|
Equals | 确定指定的对象是否等于当前对象。 (继承自 Object。) | |
Finalize | 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。) | |
GetHashCode | 用作特定类型的哈希函数。 (继承自 Object。) | |
GetType | 获取当前实例的 Type。 (继承自 Object。) | |
MemberwiseClone | 创建当前 Object 的浅表副本。 (继承自 Object。) | |
ToString | 返回表示当前对象的字符串。 (继承自 Object。) | |
Validate | 当在派生类中重写时,它既验证请求又验证响应。 |
页首
备注
用户编写的验证规则必须从此类派生。 验证规则在请求完成后立即执行。
对继承者的说明
当从 ValidationRule 继承时,您必须重写 Validate 方法和 RuleName 属性。
示例
下面的代码示例演示如何从 ValidationRule 继承,以生成验证网页上是否存在脚本的规则。
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
线程安全
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
请参见
参考
Microsoft.VisualStudio.TestTools.WebTesting 命名空间
其他资源
继承层次结构
System.Object
Microsoft.VisualStudio.TestTools.WebTesting.ValidationRule
Microsoft.VisualStudio.TestTools.WebTesting.Rules.SharePointValidationRuleFindText2
Microsoft.VisualStudio.TestTools.WebTesting.Rules.SharePointValidationRuleResponseURL2
Microsoft.VisualStudio.TestTools.WebTesting.Rules.ValidateFormField
Microsoft.VisualStudio.TestTools.WebTesting.Rules.ValidateHtmlSelectTag
Microsoft.VisualStudio.TestTools.WebTesting.Rules.ValidateHtmlTagInnerText
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
Microsoft.VisualStudio.TestTools.WebTesting.Rules.ValidationRuleResponseTimeGoal