Extend Visual Studio Web Test framework to extract JSon value

Previous post hinted about how Visual Studio Web Test framework can be used to perform Service/API level tests. These type of tests will come handy to gain more coverage, hit those end points that your GUI hits but without depending on the GUI, drive end-end or system tests with data mix combinations etc

Visual Studio Web Performance Test capability has been there for a while, mainly used in performance and load testing. However, with some tweaks, we may use the same framework to test our APIs.

First requirement could be, VS web tests handle HTML responses from the call. APIs calls typically return xml or Json responses. How to handle this? Pretty simple, VS web test framework is extensible, here is the API documentation.

Below is a sample gist how I created a custom extraction rule to extract JSon node value for my API test. Using Newtonsoft Json library, JToken.parse to parse the Json response..nothing much to explain..


using System.ComponentModel;

using Microsoft.VisualStudio.TestTools.WebTesting;

using Newtonsoft.Json.Linq;


namespace My.TestingComponents.WebTestExtensions


[DisplayName(“Extract Json node value”)]

[Description(“Extracts the value of a Json node from the response”)]

public class CustomExtractJsonNodeValue : ExtractionRule



[Description(“Json path to extract value with”)]

public string JsonNodeName { get; set; }


// The Extract method. The parameter e contains the web performance test context.

public override void Extract(object sender, ExtractionEventArgs e)


var o = JToken.Parse(e.Response.BodyString);

string propertyValue = (string)o.SelectToken(JsonNodeName);

if (propertyValue != null)


e.WebTest.Context.Add(ContextParameterName, propertyValue);

e.Message = “Successfully added ” + propertyValue + ” to the context parameter ” + ContextParameterName;

e.Success = true;




e.Message = “Couldn’t add ” + JsonNodeName + ” to the context parameter ” + ContextParameterName;

e.Success = false;







Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s