cancel
Showing results for 
Search instead for 
Did you mean: 

ReadyAPI script assertion 'is not equal' to for JSON response

SOLVED
Frequent Contributor

ReadyAPI script assertion 'is not equal' to for JSON response

Greetings,

 

I would like to assert that certain values are not present in the response of my REST request.

I'm using ReadyAPI V2.8.0.

when sending the endpoint I'm expecting only values for the language='nl'. If the language is 'fr','de' or 'en' the testcase should fail.

I receive a lot of answers for that request, so using the Smart Assertions is not an option.

I've tried regular expression, but I don't seem to understand this.

I've seen in the documentation of SmartBear that I could use Script Assertions but I don't find snippets to get to that.

  • * (Not) Contains is only looking for the node
  • * JsonPath expression is only looking for the node
  • * Equals is looking for a text (or property, but here it's not about properties) but those abreviations could be in a complete description and that is not what I need.
  •  

Does anyone have an idea please?

Response is Json and looks like this

 

Isnotpresent.png

 

You won't be able to test the endpoint as there is a strong security on it.

Thanks in advance

Kind Regards,

AboveAndBeyond

1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

Re: ReadyAPI script assertion 'is not equal' to for JSON response

Hey @AboveAndBeyond 

 

Here's the script assertion that might need a little tweak for multiple values but works to assert an attribute doesnt have a specific single value.

//courtesy of Rao
//this is when the JSONPATH to the relevant attribute is 'pdcMetadata.pdcData.concepts.description.language'
//this checks that the language attribute does NOT contain the value 'fr'
def json = new groovy.json.JsonSlurper().parseText(context.response)
def languageVals = json.pdcMetadata.pdcData.concepts.description.language
def checkFor = 'fr'
//Negative check - value should not have appointment "!=", 
//Positive check -  use "==" to match value with fr
assert languageVals.every {it != checkFor}, "Not expecting value ${checkFor} for language, but found"

As you can see above - this just verifies that the language attribute doesn't contain the value 'fr' - if you are trying to assert that language doesn't hold 'fr', nor 'en', nor 'de' - at a worst case (which is a rubbish option) you could just add multiple script assertions to cover each language option - but I think you can just stick them in an array on the checkFor variable definition - e.g.

def json = new groovy.json.JsonSlurper().parseText(context.response)
def languageVals = json.pdcMetadata.pdcData.concepts.description.language
def checkFor = ['fr', 'en', 'de'] //here I've included the values in an array cos theres >1 value to assert against
assert languageVals.every {it != checkFor}, "Not expecting value ${checkFor} for language, but found"

I haven't actually had a chance to setup something to test this yet - but hopefully it'll start you off - the 3 separate script assertions would do the job - but that is so inelegant I'm ashamed of suggesting it - also - getting it to work properly is just waaaaay more satisfying!

 

Hope this helps fella!

 

nice one

 

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 title of the post to something more descriptive? This will help people when searching for problems.

View solution in original post

6 REPLIES 6
Highlighted
Community Hero

Re: ReadyAPI script assertion 'is not equal' to for JSON response

Hey @AboveAndBeyond,

I have a script assertion that @nmrao put together for me will help i think. Im on my phone at the moment, but when im back on my laptop i'll publish the assertion Rao gave me.

Nice one,

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 title of the post to something more descriptive? This will help people when searching for problems.
Community Hero

Re: ReadyAPI script assertion 'is not equal' to for JSON response

Hey @AboveAndBeyond 

 

Here's the script assertion that might need a little tweak for multiple values but works to assert an attribute doesnt have a specific single value.

//courtesy of Rao
//this is when the JSONPATH to the relevant attribute is 'pdcMetadata.pdcData.concepts.description.language'
//this checks that the language attribute does NOT contain the value 'fr'
def json = new groovy.json.JsonSlurper().parseText(context.response)
def languageVals = json.pdcMetadata.pdcData.concepts.description.language
def checkFor = 'fr'
//Negative check - value should not have appointment "!=", 
//Positive check -  use "==" to match value with fr
assert languageVals.every {it != checkFor}, "Not expecting value ${checkFor} for language, but found"

As you can see above - this just verifies that the language attribute doesn't contain the value 'fr' - if you are trying to assert that language doesn't hold 'fr', nor 'en', nor 'de' - at a worst case (which is a rubbish option) you could just add multiple script assertions to cover each language option - but I think you can just stick them in an array on the checkFor variable definition - e.g.

def json = new groovy.json.JsonSlurper().parseText(context.response)
def languageVals = json.pdcMetadata.pdcData.concepts.description.language
def checkFor = ['fr', 'en', 'de'] //here I've included the values in an array cos theres >1 value to assert against
assert languageVals.every {it != checkFor}, "Not expecting value ${checkFor} for language, but found"

I haven't actually had a chance to setup something to test this yet - but hopefully it'll start you off - the 3 separate script assertions would do the job - but that is so inelegant I'm ashamed of suggesting it - also - getting it to work properly is just waaaaay more satisfying!

 

Hope this helps fella!

 

nice one

 

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 title of the post to something more descriptive? This will help people when searching for problems.

View solution in original post

Frequent Contributor

Re: ReadyAPI script assertion 'is not equal' to for JSON response

Hey @richie 

Thanks for your time! I'll look at it in a few....doing other stuff first, but looks good! I'll see what groovy says about this  ;-)

Cheers!

AboveAndBeyond

Frequent Contributor

Re: ReadyAPI script assertion 'is not equal' to for JSON response

Hello @richie 

Thanks for the code and your time. it works as a charm for me

 

Thanks,

Kr,

AboveAndBeyond

Community Hero

Re: ReadyAPI script assertion 'is not equal' to for JSON response

Hey @AboveAndBeyond,

I can't take credit.....that code snippet is what @nmrao gave me! 😉

Ta

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 title of the post to something more descriptive? This will help people when searching for problems.
Community Hero

Re: ReadyAPI script assertion 'is not equal' to for JSON response

@AboveAndBeyond 

 

If you're content with the response - can you please mark up the post and 'accept as solution', so people know the issue has been resolved?

 

nice one fella,

 

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 title of the post to something more descriptive? This will help people when searching for problems.
New Here?
Join us and watch the welcome video:
Top Kudoed Authors