.NET WebTest - 获取 ValidationRule 的响应字节
我需要创建一个 ValidationRule 来测试响应字节,包括相关请求的响应。获取主请求的响应字节是很容易的任务,但我无法访问相关请求的响应。我不知道该怎么做。
public class WebTest1Coded : WebTest
{
public WebTest1Coded()
{
this.PreAuthenticate = true;
}
public long ResponseBytesLength { get; set; }
public override IEnumerator<WebTestRequest> GetRequestEnumerator()
{
if ((this.Context.ValidationLevel >= Microsoft.VisualStudio.TestTools.WebTesting.ValidationLevel.Low))
{
ValidationResponseRuleNumberOfBytes validationRule3 = new ValidationResponseRuleNumberOfBytes();
this.ValidateResponseOnPageComplete += new EventHandler<ValidationEventArgs>(validationRule3.Validate);
}
WebTestRequest request1 = new WebTestRequest("http://localhost/open.aspx");
request1.ThinkTime = 2;
yield return request1;
request1 = null;
}
}
public class ValidationResponseRuleNumberOfBytes : ValidationRule
{
public override string RuleName
{
get { return "Validate Page Size"; }
}
public override string RuleDescription
{
get { return "Validates that the page is less then 300 000 bytes."; }
}
public override void Validate(object sender, ValidationEventArgs e)
{
if (e.Response.HtmlDocument != null)
{
e.Message = string.Format("Page length is {0}", e.Response.BodyBytes.Length);
e.IsValid = e.Response.BodyBytes.Length < 300000;
}
}
}
I need to create a ValidationRule to test response bytes including responses for dependent requests. It is easy task to get response bytes for main requests, but I can not access responses for dependent requests. I have no idea how to do that.
public class WebTest1Coded : WebTest
{
public WebTest1Coded()
{
this.PreAuthenticate = true;
}
public long ResponseBytesLength { get; set; }
public override IEnumerator<WebTestRequest> GetRequestEnumerator()
{
if ((this.Context.ValidationLevel >= Microsoft.VisualStudio.TestTools.WebTesting.ValidationLevel.Low))
{
ValidationResponseRuleNumberOfBytes validationRule3 = new ValidationResponseRuleNumberOfBytes();
this.ValidateResponseOnPageComplete += new EventHandler<ValidationEventArgs>(validationRule3.Validate);
}
WebTestRequest request1 = new WebTestRequest("http://localhost/open.aspx");
request1.ThinkTime = 2;
yield return request1;
request1 = null;
}
}
public class ValidationResponseRuleNumberOfBytes : ValidationRule
{
public override string RuleName
{
get { return "Validate Page Size"; }
}
public override string RuleDescription
{
get { return "Validates that the page is less then 300 000 bytes."; }
}
public override void Validate(object sender, ValidationEventArgs e)
{
if (e.Response.HtmlDocument != null)
{
e.Message = string.Format("Page length is {0}", e.Response.BodyBytes.Length);
e.IsValid = e.Response.BodyBytes.Length < 300000;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于完成了我的任务!方法是在 WebTest.GetRequestEnumerator 中的主请求的 PostRequest 上设置事件处理程序,并在那里为依赖请求设置 ValidateResponse 事件处理程序。这是代码:
I finally done my task! The way is to set up event handler on PostRequest of main request in WebTest.GetRequestEnumerator and set up ValidateResponse event handlers for dependent requests there. Here is the code:
也许手动将事件处理程序添加到相关请求中会起作用,直到出现更好的想法。
Perhaps adding the event handler manually to the dependant requests would work until a better idea presents itself.