JSON (Javascript object notation) is a serialization format (in key-value pairs) of data structures . For REST API, it is widely used for data transfer from server to client. For example, a client sends an HTTP request with below header:
- Accept: application/json
The server can respond with below sample JSON data:
{
"result": [],
"ccapiInfo": {
"createdOn": "2017-09-07T15:25:29.000Z",
"cachedOn": "2017-09-07T15:21:49.513Z",
"origin": "cache",
"canonicalLink": "http://www.myServer.com:9885/computeConsoleApi/infra1626compute1/api/v1/instance/Compute-infra1626compute1/"
}
}
with a response header of:
In this article, we will discuss how to achieve two tasks in Apache JMeter:
{
"result": [],
"ccapiInfo": {
"createdOn": "2017-09-07T15:25:29.000Z",
"cachedOn": "2017-09-07T15:21:49.513Z",
"origin": "cache",
"canonicalLink": "http://www.myServer.com:9885/computeConsoleApi/infra1626compute1/api/v1/instance/Compute-infra1626compute1/"
}
}
with a response header of:
Content-Type: application/json
In this article, we will discuss how to achieve two tasks in Apache JMeter:
- Extract JSON data using JSON Extractor (i.e., a Post Processor) with JSONPath
- Use JSR223 Assertion (i.e., an Assertion) with Groovy
JSON Extractor / JSONPath
JSONPath is the XPath for JSON. Since a JSON structure is normally anonymous, JSONPath assigns symbol $ as the root object.
Below is a side-by-side comparison of the JSONPath syntax elements with its XPath counterparts.[9]
XPath | JSONPath | Description |
/ | $ | the root object/element |
. | @ | the current object/element |
/ | . or [] | child operator |
.. | n/a | parent operator |
// | .. | recursive descent. JSONPath borrows this syntax from E4X. |
* | * | wildcard. All objects/elements regardless their names. |
@ | n/a | attribute access. JSON structures don't have attributes. |
[] | [] | subscript operator. XPath uses it to iterate over element collections and for predicates. In Javascript and JSON it is the native array operator. |
| | [,] | Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set. |
n/a | [start:end:step] | array slice operator borrowed from ES4. |
[] | ?() | applies a filter (script) expression. |
n/a | () | script expression, using the underlying script engine. |
() | n/a | grouping in Xpath |
JSONPath expressions can use the dot–notation
$.ccapiInfo.canonicalLink
or the bracket–notation
$['ccapiInfo']['canonicalLink']
for input paths. For the internal or output paths, they will always be converted to the more general bracket–notation. Below diagram shows the evaluation result using a JSONPath Online Evaluator with the input and JSONPath Expression as given in this article.
JSR223 Assertion
If you need to write scripting assertion code to extend baseline JMeter functionality, JSR223, in combination with Groovy language is a good choice performance-wise—especially when its compilation caching is enabled.
Groovy Script
String jsonString = vars.get("myCanonicalLink");
String userNameString = vars.get("user_name");
log.info ("The canonicalLink is " + jsonString);
if ( jsonString != "http://myserver.com:9885/computeConsoleApi/" +
userNameString + "/api/v1/instance/Compute-" + userNameString + "/")
{
AssertionResult.setFailureMessage("The canonicalLink is wrong");
AssertionResult.setFailure(true);
}
However, every test element including assertion added to the test plan will increase the total CPU and memory requirements. So, plan your use of assertions sparingly.
References
- How to verify JSON Response in JMeter?
- Parsing JSON responses with JMeter
- JSR223 Sampler
- How to Use JMeter Assertions in Three Easy Steps
- Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For!
- Using JMeter with JSON Responses
- JSONPath Online Evaluator
- Useful Groovy String Functions
- JSONPath - XPath for JSON
- API Testing With JMeter and the JSON Extractor
- Execution Order
- Scoping Rules
- JSON Extractor
- JMeter—Use Extractors (Post-Processor Elements) for Correlation (Xml and More)
- JMeter: How to Turn Off Captive Portal from the Recording Using Firefox (Xml and More)
- JMeter―Select on Multiple Criteria with JSONPath (Xml and More)
No comments:
Post a Comment