Groovy script for Assertion to check value
Hi
I am trying to write a script assertion, where in response after a particular "id" i have to check the status of other property,
Below is my sample response
{
"gr" : [
{
"args" : [
{
"Id" : "abcd",
"State" : "success"
},
{
"Id" : "d1234",
"State" : "failed"
},
{
"Id" : "a123",
"State" : "success"
},
{
"Id" : "T123",
"State" : "success"
},
{
"Id" : "d1234",
"State" : "success"
},
]
},
]
}
in the response the id: "d1234" repeats twice, where as i have to check the State of the "d1234" which is available after the id="T123" after research i have done below script assertions where as its checking both d1234 and the assertions is failing.
import groovy.json.JsonSlurper
def response = messageExchange.response.responseContent
def jsonsl = new JsonSlurper().parseText(response)
def res = context.expand( '${checking#Response#$[\'gr\'][0][\'args\']}' )
def Result = new JsonSlurper().parseText(res)
def Count = Result.size()
def count = 0
for ( i=0;i < Count; i++){
if (jsonsl.gr[0].args[i].'Id' == 'd1234'){
def status = jsonsl.gr[0].args[i].'State'
log.info (status)
assert status == 'success' : "d1234 failed"
count ++
}
}
assert count != 0 : "d1234 is missing"
So, Please let me know how to check the state of d1234 which is available after T123 only. The node value of T123 is not constant.
Thanks
Thanks for the Update.
I tried modifying the Code like below, its working now.. Before i was facing error. it was failing in some environments
import groovy.json.JsonSlurper def response = messageExchange.response.responseContent def jsonsl = new JsonSlurper().parseText(response) def Result = jsonsl.gr[0].args def count = Result.size() def flag = false for (int i=0;i < count; i++){ if (Result[i].Id == "abcd"){ for (int j=i ; j < count; j++){ def id = Result[j].Id if(id == "d1234"){ def status = Result[j].State assert id == "d1234" : "d1234 not found" assert status == 'success' : "d1234 failed" flag = true; } } } } assert flag , "d1234 is missing"
Thanks