"On the 10th day of DevTest the CA team brought to me 10 JSON Assertions and Filters,
NINE API tests running
EIGHT transactions documented
SEVEN well calibrated agents,
SIX agents, protocols, categories intercepted,
FIVE generated assets,
FOUR opaque data types processes,
THREE new execution modes,
TWO RR pairs added,
and a Server-Side Recorder in a DevTest tree!"
Welcome to our 10th day of DevTest 8.0! Today we are looking at the new CA Application Test capability around JSON based REST API Testing
Prior to 8.0 if you need to parse JSON formatted response from REST APIs you have very limited choices. Either you will have to convert the JSON into XML and use XPath (does not always work) or you will have to parse the response yourself. In both cases some form of coding was necessary. In 8.0 we are making JSON formatted data first class citizen. Using new set of Assertions and Filters you will be able to directly work on JSON data using JSON Path.
Quick look at JSONPath:
Like XPath JSON Path allows expression based query and filtration of JSON data. In the expression top level or root of JSON structured data is referred by $ sign while nodes are separated by dot (.) notation. For illustration purpose I will use following modified form of data from tutorial.
{ "id": 2,
"name": "An ice sculpture",
"price": 12.50,
"tags": ["cold", "ice"],
"dimensions": {
"length": 7.0,
"width": 12.0 },
"warehouseLocation": {
"latitude": -78.75
}
}
Using the dot-notation and $ as the root you could refer to length as $.dimensions.length. This expression will return 7.0. Array notation can be used to retrieve specific element of an array. In above example tags is an array and if I want to get 1st tag, I could use the expression $.tags[0] which will return "cold". You can get more info on creating JSONPath expressions from http://goessner.net/articles/JsonPath/.
Using JSON Path based filters to query data:
New JSON Path filter will allow you to add a filter as shown below.
The JSON Path Filter needs 4 attributes to be filled out. Basically input data in the form of CA Application Test property, JSON Path expression and two output property names to hold the result and result count. Following is an example of getting the names of all the objects with length less than 7.
After the filter execution is done the name(s) will be saved in property called allObj1 while count can be found in property totalObj1. Please note that the return value might be an array or a single value depending upon the query result.
JSON Path based Assertions:
We added 3 main assertions to make it easier to validate the JSON based responses, as shown below.
1st thing you would want to validate is the structure if the response conforms to the expectation. For which we added JSON Schema based validation assertion. The assertion takes Schema and the payload as input and validate if the payload is conforming the schema. Here is an example.
For design time assistance we are also proving additional details in case the data is not conforming to the schema. Here is an example of the design time assistance where schema requires property name2 be there but payload is sending name instead.
For data value validation we allow both equal and contain assertions. Equal assertion matches the result of JSON Path for exact value, as shown below we want to make sure that object with id=3 has length 3.1.
Contain assertion gives bit more flexibility for array based data. Following we are validating that an object with id=3 exists in the payload we are validating.
We believe these filters and assertions should help tremendously in testing REST based APIs. Do you have JSON based APIs? Looking forward to getting your feedback.
--------------------------------------------------------------------------------------
TThe 12 Days of DevTest Blog SeriesT
TWELVE Mobile Tests Generated
ELEVEN Selenium UI Test Scripts
TEN JSON assertions and filters
NINE API tests running
EIGHT Transactions Documented
SEVEN well calibrated agents
SIX Agents, protocols, categories intercepted
FIVE Generated assets
FOUR Opaque data types processed
THREE new execution modes
TWO RR pairs added
and A Server-Side Recorder in a DevTest tree!"
--------------------------------------------------------------------------------------