Groovy script for Assertion to check value
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You were confident earlier regarding order despite saying. Can you go back to previous replies and identify how you can uniquely identify, logic can be written accordingly.
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@doubtsreadyapi : Have you tried modifying the code which i provided earlier, if you try to modify that code you will be able to assert on your new requirement.
Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓
Thanks and Regards,
Himanshu Tayal
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Glag that you solved at your own, that way you will learn groovy 🙂
Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓
Thanks and Regards,
Himanshu Tayal

- « Previous
-
- 1
- 2
- Next »
- « Previous
-
- 1
- 2
- Next »