Skip to content

Instantly share code, notes, and snippets.

@RichardSlater
Created January 28, 2014 15:31
Show Gist options
  • Save RichardSlater/8669705 to your computer and use it in GitHub Desktop.
Save RichardSlater/8669705 to your computer and use it in GitHub Desktop.
Visual Studio Test Tools JSON Extraction Rule using JSON.net
using System;
using System.ComponentModel;
using System.Globalization;
using Microsoft.VisualStudio.TestTools.WebTesting;
using Newtonsoft.Json.Linq;
namespace Amido.PerformanceTests.Common {
[DisplayName("JSON Extraction Rule")]
[Description("Extracts the specified JSON value from an object.")]
public class JsonExtractionRule : ExtractionRule {
public string Name { get; set; }
public override void Extract(object sender, ExtractionEventArgs e) {
if (e.Response.BodyString != null) {
var json = e.Response.BodyString;
var data = JObject.Parse(json);
if (data != null) {
e.WebTest.Context.Add(this.ContextParameterName, data.SelectToken(Name));
e.Success = true;
return;
}
}
e.Success = false;
e.Message = String.Format(CultureInfo.CurrentCulture, "Not Found: {0}", Name);
}
}
}
@zavier-sanders
Copy link

Is this supposed to always "Passed" during the test? I cannot make this test fail. Even when I enter a value into "Name" that doesn't exist in the response, the test still passes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment