Dependent request filter for Web tests


A pretty common need when you are testing your web application is to filter out certain types of dependent requests. For example, if a page you are testing has an ad in it from an external site, you may not want to hit that site in your load tests.

Here is a simple WebTestPlugin that will allow you to filter any dependents that start with a particular string, so you can then easily filter all dependents that go to a particular external site.

using Microsoft.VisualStudio.TestTools.WebTesting;

 

namespace SampleWebTestRules

{

    public class WebTestDependentFilter : WebTestPlugin

    {

        string m_startsWith;

        public string FilterDependentRequestsThatStartWith

        {

            get { return m_startsWith; }

            set { m_startsWith = value; }

        }

 

        public override void PostRequest(object sender, PostRequestEventArgs e)

        {

            WebTestRequestCollection depsToRemove = new WebTestRequestCollection();

 

            // Note, you can’t modify the collection inside a foreach, hence the second collection

            // requests to remove.

            foreach (WebTestRequest r in e.Request.DependentRequests)

            {

                if (!string.IsNullOrEmpty(FilterDependentRequestsThatStartWith) &&

                    r.Url.StartsWith(FilterDependentRequestsThatStartWith))

                {

                    depsToRemove.Add(r);

                }

            }

            foreach (WebTestRequest r in depsToRemove)

            {

                e.Request.DependentRequests.Remove(r);

            }

        }

    }

}

 

Ed.

Comments (3)

  1. Tal Peer says:

    Thanks very much for this worked a treat!!!

  2. Ian Holdsworth says:

    Hi Ed

    This works great for me most of the time, however, it doesn't appear to parse dependent requests themselves.  i.e. It will parse my primary request but any dependencies it lets through will not be parsed by this plugin.

    Hope this makes sense