Forum Discussion

jsontester's avatar
jsontester
Occasional Contributor
4 years ago

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"
}
]

 
  • 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

     

  • richie's avatar
    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
  • 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