Created
January 28, 2014 15:31
-
-
Save RichardSlater/8669705 to your computer and use it in GitHub Desktop.
Visual Studio Test Tools JSON Extraction Rule using JSON.net
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.