我需要创建一个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;
}
}
}
答案 0 :(得分:1)
我终于完成了我的任务!方法是在WebTest.GetRequestEnumerator中的主请求的PostRequest上设置事件处理程序,并为那里的依赖请求设置ValidateResponse事件处理程序。这是代码:
public class PageSize : WebTest
{
public PageSize()
{
this.PreAuthenticate = true;
}
public int DependentResponsesBytes { get; set; }
public long ResponseBytesLength { get; set; }
public long ResponseBytesLengthMaximum { 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;
DependentResponsesBytes = 0;
ResponseBytesLengthMaximum = 300000;
request1.PostRequest += new EventHandler<PostRequestEventArgs>(SetValidationEventsForDependentResponses);
yield return request1;
request1 = new WebTestRequest("http://localhost/welcome.aspx");
request1.ThinkTime = 2;
DependentResponsesBytes = 0;
ResponseBytesLengthMaximum = 400000;
request1.PostRequest += new EventHandler<PostRequestEventArgs>(SetValidationEventsForDependentResponses);
yield return request1;
request1 = null;
}
void SetValidationEventsForDependentResponses(object sender, PostRequestEventArgs e)
{
foreach (WebTestRequest depententRequest in e.Request.DependentRequests)
{
depententRequest.ValidateResponse += new EventHandler<ValidationEventArgs>(depententRequest_ValidateResponse);
}
}
void depententRequest_ValidateResponse(object sender, ValidationEventArgs e)
{
DependentResponsesBytes += e.Response.BodyBytes.Length;
}
}
public class ValidationResponseRuleNumberOfBytes : ValidationRule
{
public override string RuleName
{
get { return "Validate Page Size"; }
}
public override string RuleDescription
{
get { return string.Format("Validates that the page is less then some number of bytes set in WebTest."); }
}
public override void Validate(object sender, ValidationEventArgs e)
{
if (e.Response.HtmlDocument != null)
{
int totalBytes = e.Response.BodyBytes.Length + ((PageSize)e.WebTest).DependentResponsesBytes;
e.Message = string.Format("Page length is {0}", totalBytes);
if (totalBytes < ((PageSize)e.WebTest).ResponseBytesLengthMaximum)
{
e.IsValid = true;
e.Message = "Passed";
}
else
{
e.IsValid = false;
e.Message = "Failed";
}
e.Message += string.Format(". Page length is {0} bytes. Maximum is {1}", totalBytes, ((PageSize)e.WebTest).ResponseBytesLengthMaximum);
}
}
}
答案 1 :(得分:0)
或许手动将事件处理程序添加到依赖请求中,直到有更好的想法呈现为止。
foreach (WebTestRequest request in request1.DependentRequests)
{
request.ValidateResponse += new EventHandler<ValidationEventArgs>(validationRule3.Validate);
}