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
- It is surprising that how will same id will have different status. Isn't it?
- Is it ok to get all the matching status'es for the given id and see if it has a success?
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- It is surprising that how will same id will have different status. Isn't it? Yes some times both the status will be same some times it may vary.
- Is it ok to get all the matching status'es for the given id and see if it has a success? The requirement is to check the state of the id:d1234 after T123 only.
I am not sure how to put the condition using groovy script to get the state of the id:d1234 comes after T123. Please help
Thanks
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi @doubtsreadyapi ,
Please try below groovy hope it will help you out:
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 == "T123"){
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"
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
What if both d1234 are present after T123 with status'es failed and success?
Clear some context is missing from the original test or due to simplification of the problem.
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
https://stackoverflow.com/questions/3948206/json-order-mixed-up/4920304#4920304
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am glad that it worked 🙂 👍
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
Hi @HimanshuTayal
I previous post
As i mentioned earlier my sample response like below,
{
"gr" : [
{
"args" : [
{
"Id" : "abcd",
"State" : "success"
},
{
"Id" : "d1234",
"State" : "failed"
},
{
"Id" : "a123",
"State" : "success"
},
{
"Id" : "T123",
"State" : "success"
},
{
"Id" : "d1234",
"State" : "success"
},
]
},
]
}
Now i have to keep the assertion for first "d1234" parameter, here the
problem is in some environments "d1234" only comes once which is after "T1234"
Which we already done assertion, Now how to handle the first "d1234" which is coming
after "abcd" but this "d1234" will not come in some environments as per requirements
So still the assertion must get pass in other environments if the "d1234" does not exist also.
Please help me how we can achieve it.
Thanks
