Assertion for 1 specific value which contains in all results in response
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 })
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Can you tell me how to use one-liner script assertion? From "Add Assertion" and then "Script Assertion" does not work.
With JSONPath Expression: "$['data'][*]['postalCode']" the assertion likes ok. But the expected value can change from example 70 result to 71 when add 1 postalCode and the expected result is different.
I want to check all the postalCode have the value "1520"
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Can you confirm whats the problem with the script assertion?
Reason i ask is that @nmrao gave you the code and some alternatives for you to add to a script assertion which would be perfect for what you appear to be asking for.
The jsonpath match assertion is good for individual specific field testing but for the scenario i thought you described you wanted to assert a repeating specific postcode field in your payload would contain specific values.
Have i misunderstood?
Cheers,
Rich
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It would be helpful if you can show what exactly you have tried and the issue being faced.
NOTE: there is no sample response provided in the question. the solution was provided based on the jsonpath expression provided.
- It does not matter if the script is one-liner or not, the script assertion can be used in the same manner.
- If there is still an issue with script, request you to provide the sample response.
- Thank you for the screenshot. I am not good at jsonpath, however, for varying results, static expected result can't be used to verify.
If need more information on Script Assertion, refer below documentation
https://support.smartbear.com/readyapi/docs/testing/assertions/reference/script.html
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
example of my response:
{
"data": [{
"City": "New York",
"houseNumber": 1,
"postalCode": 1520
},
{
"City": "New York",
"houseNumber": 2,
"postalCode": 1520
},
{
"City": "New York",
"houseNumber": 2,
"postalCode": 1520
},
{
"City": "New York",
"houseNumber": 4,
"postalCode": 1520
},
{
"City": "New York",
"houseNumber": 5,
"postalCode": 1520
}
]
}
What I want is:
- check if value all "postalCode" are equal to "1520"
- check whether value all "postalCode" is greater than "1519"
Also I want to check if value of "City" is equal to "New York"
Which assertion method can I use?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- check whether value all "postalCode" is greater than "1519"
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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"})
Regards,
Rao.
