Ask a Question

Assertion for 1 specific value which contains in all results in response

SOLVED
jsontester
Occasional Contributor

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

11 REPLIES 11
nmrao
Community Hero

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.
jsontester
Occasional Contributor

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"

jsontester_0-1619792028235.png

 

 

 

Hey @jsontester,

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
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta

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.
jsontester
Occasional Contributor

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?

You mentioned earlier that you were getting error for script assertion, can you show that please?


Regards,
Rao.

City will have fixed postal code. So, checking for exact value make sense and below doesn't have any significance.
- check whether value all "postalCode" is greater than "1519"


Regards,
Rao.
jsontester
Occasional Contributor

jsontester_0-1620054964002.png

 

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.
cancel
Showing results for 
Search instead for 
Did you mean: