Require condition to check depending upon the Json Node response
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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")
}
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Its a cracking response....explains each point (e.g.trying to use xpath in json, etc.) and lays it all out very nicely.
Rich
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
hi @ChrisA
Thanks for the detailed reply. I have written the code like this.
def response = context.expand( '${CurrentSprint#Response#$[\'values\'][0][\'id\']}' )
log.info response
if (response != null)
{
testRunner.testCase.testSuite.testCases["Jira Executions"].setPropertyValue("Active1", "$response")
}
else
{
testRunner.testCase.testSuite.testCases["Jira Executions"].setPropertyValue("Active", Empty)
}
Where as def response = context.expand( '${CurrentSprint#Response#$[\'values\'][0][\'id\']}' ) the id will be coming blank some times in the groovy response, So i wrote if loop, to save the value in custom properties, where as when the response is null or not null its checking only condition.
Attached is the groovy response..
So if the response is null i have to Set custom propery as string empty, other wise what ever the value is coming in the response that value i have to store in the custom properties
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Awesome job, Community!
@doubtsreadyapi could you let us know if some more help is required here from the community members or if the code you came up with works for your case? Thank you.
Sonya Mihaljova
Community and Education Specialist
