Require condition to check depending upon the Json Node response
Hi,
Depending upon the json node, for a json node if the key exists and value exists i have to set property in custom properties and if the key value doesnt exist then i have to set the property with null value.
My Same response with the Key value exists in response for a Json node.
{
"maxResults" : 50,
"startAt" : 0,
"isLast" : false,
"values" : [
{
"id" : 221,
"startDate" : "2018-06-26T20:00:16.850Z",
"endDate" : "2018-07-09T21:00:00.000Z",
}
]
}
in Above json response, if the id exists and the value for the id i must set to a custom property
Sample response for which for json node values. id doesent exists, in this scenario i have to set custom property as null
{
"maxResults" : 50,
"startAt" : 0,
"isLast" : true,
"values" : [ ]
}
i tried below script, but its not working as expected.
def response_id = context.expand( '${CurrentSprint#Response#$[\'values\'][0][\'id\']}' )
if (response_id !=null)
{
testRunner.testCase.testSuite.testCases["Test"].setPropertyValue("Active", "$response_id")
}
else
{
testRunner.testCase.testSuite.testCases["TEST"].setPropertyValue("Active","null")
}
Hi,
Firstly, have you tried logging the response to see what you have?
def response_id = context.expand( '${CurrentSprint#Response#$[\'values\'][0][\'id\']}' ) log.info(response_id); if (response_id !=null)
XPath is more for XML response and not JSON. If you want to use XPath, then maybe pull the response as XML. E.g.
def response_id = context.expand( '${TestStepName#ResponseAsXml#$[\'values\'][0][\'id\']}' )
I haven't tested this by the way, but that should convert the response to XML so you can traverse with XPath.
Instead, you could convert the response to JSON using JSON Slurper and interrogate as a JSON Object....
import groovy.json.JsonSlurper; def slurper = new groovy.json.JsonSlurper(); // Obviously, you would need to pull the string from the response first. def result = slurper.parseText('{"maxResults" : 50,"startAt" : 0,"isLast" : false,"values" : [{"id" : 221,"startDate" : "2018-06-26T20:00:16.850Z","endDate" : "2018-07-09T21:00:00.000Z"}]}'); // Can we get ID? log.info(result.values.id); return result.values.id;
Re setting property value, I'd imagine that .testCases["Test"] is different from .testCases["TEST"]. Is your test case called 'Test' or 'TEST'.
Here's an example based on the JSON in your question on how to check and set Property Value if the id is there or not.
import groovy.json.JsonSlurper; def slurper = new groovy.json.JsonSlurper(); def result = slurper.parseText('{"maxResults" : 50,"startAt" : 0,"isLast" : false,"values" : [{"id":221, "startDate" : "2018-06-26T20:00:16.850Z","endDate" : "2018-07-09T21:00:00.000Z"}]}'); // Can we get ID? log.info(result.values[0].id); // Check for Id.... if (result.values[0].id){ def id = Integer.toString(result.values[0].id); testRunner.testCase.testSuite.testCases["Grab JSON - TestCase"].setPropertyValue("Active", "$id"); } else { testRunner.testCase.testSuite.testCases["Grab JSON - TestCase"].setPropertyValue("Active", "Not Found"); } return result;