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}