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 ...
  • HimanshuTayal's avatar
    4 years ago

    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