tags:

views:

9

answers:

2

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;                
        }
    }
}
A: 

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);
}
Nat
Thank you Nat! I had this idea to attach validation rule to each of the dependent request. But dependent requests add automatically by main request (and I want to keep it this way). So, I can not attach validation rule when overriding GetRequestEnumerator of WebTest as dependent requests are not populated. I tried to find appropriate event for that but failed with either it is not populated or it is too late to attach to validate event of the request.
Denis
+1  A: 

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);
        }
    }
}
Denis
Adding the validation code to a WebTestPlugin may help to clean up the code and make it re-usable too.
Nat