cancel
Showing results for 
Search instead for 
Did you mean: 

how to put assertion, if i want only exact match, eg 42,then if response contain 142, it should fail

New Contributor

how to put assertion, if i want only exact match, eg 42,then if response contain 142, it should fail

Hi SmartBear Team,

 

I have to put contain assertion with any no. value or text , and my requirement is, it should pass only if that number or text exact match.

For example if i need 42 or "ear" in response, and response contain 142 or "early".  Then it should fail.

6 REPLIES 6
Community Hero

Re: how to put assertion, if i want only exact match, eg 42,then if response contain 142, it should

Get hold of the whole value using XPath (Xpath assertion, script assertion), XmlSlurper (script assertion), Regular Expression (contains assertion, script assertion), JsonPath (JsonPath assertion), JsonSlurper (script assertion).

 

Then just make a direct comparison.

 

However, if you want a specific answer, please ask a specific question. What format is your data in? Can you give an example (i.e. an entire response message) that should match and one that shouldn't match?

New Contributor

Re: how to put assertion, if i want only exact match, eg 42,then if response contain 142, it should

Hi,

 

I get response in json format (REST API)

Community Hero

Re: how to put assertion, if i want only exact match, eg 42,then if response contain 142, it should

 assert new groovy.json.JsonSlurper()
  .parseText(messageExchange.responseContent)
  .find {it.key == "abc" && it.value == 123}
New Contributor

Re: how to put assertion, if i want only exact match, eg 42,then if response contain 142, it should

I have a doubt here, i need to add a groovy script step here, and need to add content given by you. Or directly i can keep it either contains or Not Contains assertion section

Community Hero

Re: how to put assertion, if i want only exact match, eg 42,then if response contain 142, it should

Above is for script assertion.

Frequent Contributor

Re: how to put assertion, if i want only exact match, eg 42,then if response contain 142, it should

@JHunt 

Thanks for helping out. But for me this script isn't working completely as I would. Could you help me out please?

 

I'm working with REST request with responses in JSON. If I use this Script Assertion it fails because there are other values present. So this is great! BUT:

The error message is just throwing me the whole response content. I would expect that the script just tells me which values shouldn't be in there. So actually something like:

if you found anything else then IT

            show this in the Script assertion error message.

 

Is this possible please? Or maybe even in a popup. Is also cool....

 

errormessageAssertionScript.png

 

Thanks in advance for looking into this.

Kind regards,

AAB

New Here?
Join us and watch the welcome video:
6 / 7 API Testing Mistakes Video
Top Kudoed Authors
Top Kudoed Posts