ValidationRule.Validate 方法
当在派生类中重写时,它既验证请求又验证响应。
命名空间: Microsoft.VisualStudio.TestTools.WebTesting
程序集: Microsoft.VisualStudio.QualityTools.WebTestFramework(在 Microsoft.VisualStudio.QualityTools.WebTestFramework.dll 中)
语法
声明
Public MustOverride Sub Validate ( _
sender As Object, _
e As ValidationEventArgs _
)
public abstract void Validate(
Object sender,
ValidationEventArgs e
)
public:
virtual void Validate(
Object^ sender,
ValidationEventArgs^ e
) abstract
abstract Validate :
sender:Object *
e:ValidationEventArgs -> unit
public abstract function Validate(
sender : Object,
e : ValidationEventArgs
)
参数
sender
类型:Object事件源。
备注
用测试用例上下文和事务上下文调用 Validate。 它们允许规则既检查请求又检查响应并确保数据与所需的值匹配。
示例
下面的示例验证网页是否包含任何客户端脚本。
namespace TestProject1
{
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.WebTesting;
using Microsoft.VisualStudio.TestTools.WebTesting.Rules;
using MyValidationRule;
public class MyCodedWebTest : WebTest
{
public override IEnumerator<WebTestRequest> GetRequestEnumerator()
{
WebTestRequest request1 = new WebTestRequest
("https://localhost/MyWebSite");
request1.ThinkTime = 13;
ExtractHiddenFields rule1 = new ExtractHiddenFields();
rule1.ContextParameterName = "1";
request1.ExtractValues += new EventHandler<ExtractionEventArgs>
(rule1.Extract);
yield return request1;
WebTestRequest request2 = new WebTestRequest
("https://localhost/MyWebSite/Default.aspx");
request2.Method = "POST";
FormPostHttpBody request2Body = new FormPostHttpBody();
request2Body.FormPostParameters.Add("__VIEWSTATE",
"{{$HIDDEN1.__VIEWSTATE}}");
request2Body.FormPostParameters.Add("Button1", "Button");
request2Body.FormPostParameters.Add("TextBox1",
"Added Text in form");
request2.Body = request2Body;
ValidatePageContainsScript rule2 = new ValidatePageContainsScript();
request2.ValidateResponse += new EventHandler<ValidationEventArgs>
(rule2.Validate);
yield return request2;
}
}
}
Option Strict Off
Option Explicit On
Imports Microsoft.VisualStudio.TestTools.WebTesting
Imports Microsoft.VisualStudio.TestTools.WebTesting.Rules
Imports MyVBProject.MyValidationRule
Imports System
Imports System.Collections.Generic
Namespace MyVBTestProject
Public Class MyCodedWebTest
Inherits ThreadedWebTest
Public Sub New()
MyBase.New
Me.PreAuthenticate = true
Me.Proxy = "myproxy.com:80"
End Sub
Public Overrides Sub Run()
Dim request1 As WebTestRequest = New WebTestRequest _
("https://localhost/ts")
request1.ThinkTime = 18
Dim rule1 As ValidatePageContainsScript = New _
ValidatePageContainsScript
AddHandler request1.ValidateResponse, AddressOf rule1.Validate
Dim rule2 As ExtractHiddenFields = New ExtractHiddenFields
rule2.ContextParameterName = "1"
AddHandler request1.ExtractValues, AddressOf rule2.Extract
MyBase.Send(request1)
End Sub
End Class
End Namespace
.NET Framework 安全性
- 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关详细信息,请参阅通过部分受信任的代码使用库。