Forum Discussion

jsontester's avatar
jsontester
Occasional Contributor
3 years ago
Solved

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 })

     

     

  • nmrao's avatar
    nmrao
    3 years ago

    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"})

11 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    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 })

     

     

    • jsontester's avatar
      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"

       

       

       

      • richie's avatar
        richie
        Community Hero
        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