.NET WebTest - 获取 ValidationRule 的响应字节

发布于 2024-09-27 02:57:41 字数 1532 浏览 5 评论 0原文

我需要创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神仙妹妹 2024-10-04 02:57:41

我终于完成了我的任务!方法是在 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);
        }
    }
}

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:

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);
        }
    }
}
蒗幽 2024-10-04 02:57:41

也许手动将事件处理程序添加到相关请求中会起作用,直到出现更好的想法。

foreach (WebTestRequest request in request1.DependentRequests)
{
    request.ValidateResponse += new EventHandler<ValidationEventArgs>(validationRule3.Validate);
}

Perhaps adding the event handler manually to the dependant requests would work until a better idea presents itself.

foreach (WebTestRequest request in request1.DependentRequests)
{
    request.ValidateResponse += new EventHandler<ValidationEventArgs>(validationRule3.Validate);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文