Dependent Request Extraction VSTS
Sometimes your extraction fails because what you are looking for is returned in a dependent request. Here is a plug-in you can use to scan dependent requests. You can adjust the below example to search the body or headers by changing “string body = e.Response .Headers.ToString();”. In the example the plugin is searching headers only.
[Description("Extracts Text from the Response of a Dependent Request or any redirect using regex group")]
public class FindTextInDependentRequestResponse : WebTestRequestPlugin
{
Match match;
public override void PostRequest(object sender, PostRequestEventArgs e)
{
//Create a collection of all dependent requests
WebTestRequestCollection dependents = e.Request.DependentRequests;
//Gets the request that matches the filter URL and kicks off the postrequest event
dependents
.Where (d => d.Url == URLFilter)
.Select (d => d)
.ToList()
.ForEach(d => {
d.Cache = false;
d.PostRequest += new EventHandler<PostRequestEventArgs>(this.PostDependentRequest);
});
}
public void PostDependentRequest(object sender, PostRequestEventArgs e)
{
if (e.ResponseExists)
{
string body = e.Response.Headers.ToString();
match = Regex.Match(body, RegexPattern, RegexOptions.Singleline);
if (match != Match.Empty)
{
//Set value to equal the matched Regex
string value = match.Groups[1].Value;
//Add context param and set to value
e.WebTest.Context[TargetContextParam] = value;
//Add comment that the context param was created and a value was added
e.WebTest.AddCommentToResult(
String.Format("Found Regex and created Param {0}",TargetContextParam)
);
}
else
{
e.WebTest.AddCommentToResult(
String.Format("Could not find Regex to create Param {0}",TargetContextParam)
);
}
}
}
[Description("The name of the context parameter to store the value")]
public string TargetContextParam { get; set; }
[Description("The Regex Pattern")]
public string RegexPattern { get; set; }
[Description("The url of the target dependent request")]
public string URLFilter { get; set; }