New in Orcas! Filtering Dependent Requests

One of the new Web Test features in Orcas is the ability to filter dependent requests. If you have a request in your webtest that fetches a lot of content such as images, javascript files, css files, etc. you'll be able to programatically determine which requests are allowed to execute during the course of the web test, and which aren't. If for example you want to fetch your script and css files but don't want to fetch the images you could filter them out of the WebTestRequest.DependentRequests collection before the requests in that collection are executed by the web test engine. Previously in VS 2005 it was all or nothing, parsing dependent requests was either on or off and there was no ability to fetch some but not others.

 

The relevant order of execution in the web test engine with regards to dependent filtering looks like this:

  1. Top Level Request Runs
  2. DependentRequests collection on the Request is populated now
  3. Top Level Requests PostRequest event is fired
  4. Dependent Requests Run.

So if you want to filter requests you actually have to do it in the Top level requests PostRequest event handler rather than the PreRequest which may seem more intuitive at first.

Now on to the code sample, below is a very simple coded web test. It has one request which is against www.msn.com. As you can imagine this site loads a bunch of dependent requests but in my example below I’ll iterate over the dependent requests and remove all the gif and jpg requests from the collection. The code should probably be cleaned up a bit and could be written more efficiently for use in a Loadtest I’m sure but as a quick sample it should demonstrate the concept.

namespace TestProject1

{

    using System;

    using System.Collections.Generic;

    using System.Text;

    using Microsoft.VisualStudio.TestTools.WebTesting;

    public class WebTest1Coded : WebTest

    {

        public WebTest1Coded()

        {

            this.Context.Add("FilterOutImages", "true");

            this.PreAuthenticate = true;

        }

        public override IEnumerator<WebTestRequest> GetRequestEnumerator()

        {

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

            request1.ThinkTime = 1;

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

            yield return request1;

            request1 = null;

        }

        void request1_PostRequest(object sender, PostRequestEventArgs e)

        {

            //I'll store the index within the collection for each dependent I

            //want to remove from the collection.

            List<int> dependentsToRemoveByIndex = new List<int>();

            //Now we'll iterate over the DependentRequests collection and

            //Examine each one and decide whether we want to remove it

            //or not, in this example I'm removing anything that has a

            //url that ends with .gif or .jpg.

            for(int i=0; i < e.Request.DependentRequests.Count; i++)

            {

                string requestUrl = e.Request.DependentRequests[i].Url;

                if (requestUrl.EndsWith(".gif") || requestUrl.EndsWith(".jpg"))

                {

                    dependentsToRemoveByIndex.Add(i);

                }

            }

            //Now I'll iterate over my collection of indexes to remove and

            //remove them from the DependentRequests collection.

            for (int i = dependentsToRemoveByIndex.Count - 1; i >= 0; i--)

            {

                e.Request.DependentRequests.RemoveAt(dependentsToRemoveByIndex[i]);

            }

        }

    }

}