Another new addition to the API is the DependentRequests collection which is a property of the WebTestRequest object.  This collection gives you complete access to all Dependent Requests of a top-level request.  You can use this access to attach PreRequest and PostRequest event handlers to your dependent requests, as well as add or remove dependent requests from the collection before they are executed. 

Here’s an example of how I could use the DependentRequests collection to remove all images from the dependents, while leaving any other type of dependent (js, css, etc.).  Note that you need to manipulate the collection in the PostRequest event rather than the PreRequest event.  In the PreRequest event the request doesn’t yet know what it’s dependent requests are.  The PostRequest event is fired after the top-level request has been executed but before any of its dependents have been executed.

public class WebTest3Coded : WebTest

{

 

    public WebTest3Coded()

    {

        this.PreAuthenticate = true;

        this.Proxy = "myProxy";

    }

 

    public override IEnumerator<WebTestRequest> GetRequestEnumerator()

    {

        if ((this.Context.ValidationLevel >= Microsoft.VisualStudio.TestTools.WebTesting.ValidationLevel.Low))

        {

            ValidateResponseUrl validationRule1 = new ValidateResponseUrl();

            this.ValidateResponse += new EventHandler<ValidationEventArgs>(validationRule1.Validate);

        }

 

        WebTestRequest request1 = new WebTestRequest("http://www.msn.com/");

        request1.ThinkTime = 2;

        request1.PostRequest += new EventHandler<PostRequestEventArgs>(request1_PostRequest);

        yield return request1;

    }

 

    void request1_PostRequest(object sender, PostRequestEventArgs e)

    {

        List<WebTestRequest> remove = new List<WebTestRequest>();

        foreach (WebTestRequest dependent in e.Request.DependentRequests)

        {

            if (dependent.Url.EndsWith(".gif") || dependent.Url.EndsWith(".jpg"))

            {

                remove.Add(dependent);

            }

        }

 

        foreach (WebTestRequest dependent in remove)

        {

            e.Request.DependentRequests.Remove(dependent);

        }

    }

}