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.