Forum Discussion

doubtsreadyapi's avatar
doubtsreadyapi
Contributor
4 years ago
Solved

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

 

 

  • doubtsreadyapi's avatar
    doubtsreadyapi
    4 years ago

    Hi HimanshuTayal 

     

    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

14 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    It is not guaranteed always in the list that it will occur in the same place. it needs to different strategy.

    - 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?

    • doubtsreadyapi's avatar
      doubtsreadyapi
      Contributor

      nmrao 

       

      - 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

       

      • HimanshuTayal's avatar
        HimanshuTayal
        Community Hero

        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"