Forum Discussion

richie's avatar
richie
Community Hero
6 years ago
Solved

How to Assert Repeating Json Attributes Have The Same Value?

Hey!

 

I am testing a REST service and some of my GET request's Query Parameters actually appear as an attribute in my .json response (obviously) filtering the request.  

 

e.g. if request is GET --> whatever/rich?VersionNumber=1

 

my resultset returns records where VersionNumber=1 and furthermore includes a VersionNumber field in the response.

 

e.g.

 

$['data'][0]['VersionNumber']=1

$['data'][1]['VersionNumber']=1

$['data'][2]['VersionNumber']=1

 

I'm trying to put a nifty assertion together (I don't know if it's possible) so that I can assert that each of the VersionNumber attributes in the .json response is identical to the VersionNumber QueryParameter in my REST request. 

 

If I select the 'Select Content' button, I can navigate through to the properties associated with my testcase and select the VersionNumber property which generates the '${REST Request#VersionNumber}' parm in the expected results.

 

This would be perfect if there was only 1 record returned in the resultset - but there can be ANY number - I need to be able to assert that EVERY 'VersionNumber' attribute in the resultset has the same value as the QueryParameter VersionNumber value (or at the least - all VersionNumber attributes have the SAME value - even if I can't map it directly back to the VersionNumber QueryParameter property value.)

 

Would anyone know how to do this?

 

I found this --> which looks like someone else asked the same question BUT it was for XML - but this works for xml - I don't think it works for json:(

 

I hope I've been clear!

 

Many thanks!

 

richie

  • richie ,

     

    Thank you.

     

    Below is the script assertion which will fetch all the VersionNumber's from data array and check each value is 1

     

    assert context.response, 'Response is empty or null'
    def json = new groovy.json.JsonSlurper().parseText(context.response)
    assert json.data.VersionNumber.every{1 == it}

6 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    richie,

    It would always be helpful for quick & better understanding if explanation associated with a sample data (not really be the actual data) resembling the same structure that you need to work with. Because, people do not have to imagine or easy to help by copying your data and apply logic reducing their time.

    • richie's avatar
      richie
      Community Hero

      you're right - I know better than that  - so apologies!

       

      Please find attached an example response - I removed most of the records - but I left 3 in there so you can see the repeating pattern.

       

      thanks man,

       

      richie

      • nmrao's avatar
        nmrao
        Champion Level 3

        richie ,

         

        Thank you.

         

        Below is the script assertion which will fetch all the VersionNumber's from data array and check each value is 1

         

        assert context.response, 'Response is empty or null'
        def json = new groovy.json.JsonSlurper().parseText(context.response)
        assert json.data.VersionNumber.every{1 == it}