Assertion for 1 specific value which contains in all results in response
After a GET request where I'm searching for a specific postal code, I get response with 75 results.
I want to assert that all 70 results have the same value for property(postalCode).
If I use the assertions contains option I see JSONPatch Expression:
$['data'][0]['postalCode']
"[0]"shows the first result.
In the expected result I see value "1520"
How Can I assert that all postalCode have the same value "1520"?
What I have tried:
replacing
$['data'][0]['postalCode']
with
$['data'][*]['postalCode']
works but when the result is not 70 the assertion will fail
Strange. Not sure why problem with 70 results?
Would it be possible to share the screenshot of the assertion with expression and expected result?
Are you OK to use below one-liner script assertion?
#Checks every postalCode is 1520
assert (new groovy.json.JsonSlurper().parseText(context.response).data.postalCode.every{it == 1520})
or
#Data other than postalCode 1520 is zero
assert new groovy.json.JsonSlurper().parseText(context.response).data.postalCode.findAll {it != 1520 }.size() == 0
or even this
assert !(new groovy.json.JsonSlurper().parseText(context.response).data.postalCode.findAll {it != 1520 })
Verifying city name for all the values is "New York", you can do in the same way as previous assertion i.e., check all the values are equal to mentioned values.
You can add another assertion for the test step
assert (new groovy.json.JsonSlurper().parseText(context.response).data.City.every{it == "New York"})
- You have to use Script Assertion, not Json Path Match Assertion.
Refer
https://support.smartbear.com/readyapi/docs/testing/assertions/reference/script.html