Ask a Question

Assertion multiple objects for the same value

SOLVED
jsontester
Occasional Contributor

Assertion multiple objects for the same value

Hi, Sometimes I get about 70 objects in my response. I want assert different values and it takes a lot of time to do it for all if I have to do it separately.
I'm looking for a faster way to assert but I can't find the option in ReadyAPI. Maybe with a (groovy) script it's possible but I am not familiar with scripting.

 

Assertion for count "Employee":

 

-Count for value "Employee". If I choose assertion for count I can assert count for "role" and not for "Employee".
Expected result for count "Employee" must be 3.

 

Example response:

[
{
"id" : "00123456789",
"name" : "Mike",
"role" : "Employee",
"city" : "New York"
},
{
"id" : "00123456711",
"name" : "Jack",
"role" : "Employee",
"city" : "Arizona"
},
{
"id" : "00123456799",
"name" : "Bruce",
"role" : "Employee",
"city" : "Houston"
},
{
"id" : "00123456766",
"name" : "wilma",
"role" : "Owner",
"city" : "Texas"
}
]

 
2 REPLIES 2
richie
Community Hero

Hey @jsontester,

The Message Content Assertion might help as allows you to specify if values are > or < or = a certain value for all the attributes in your payload, however asserting on multiple attributes is always a bit lengthy and tedious.

You can use groovy to help like with your example, but only if the same values are repeated throughout your payload

Ta

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
HimanshuTayal
Community Hero

@jsontester : You can refer below code and implement the logic according to your need

 

import groovy.json.JsonSlurper

def response = testRunner.testCase.getTestStepByName("TEST__STEP__NAME").getPropertyValue("Response")

slurperRes = new JsonSlurper().parseText(response)
def val = slurperRes.PATH__TO__Array
int countEmp = 0
int countOwner = 0
for(int i = 0 ; i < val.size() ; i++){
     if(val[i].role == "Employee"){
          countEmp = countEmp+1
     }
     else if(val[i].role == "Owner"){
          countOwner = countOwner + 1
     }     
}
assert countEmp == 3 : "Employee count is not 3"

 

What i have understood i have implemented. Please let me know in case you need more help

 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
cancel
Showing results for 
Search instead for 
Did you mean: