Forum Discussion

reyaz079's avatar
reyaz079
New Contributor
7 years ago

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

  • JHunt's avatar
    JHunt
    Community Hero

    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?

    • reyaz079's avatar
      reyaz079
      New Contributor

      Hi,

       

      I get response in json format (REST API)

  • JHunt's avatar
    JHunt
    Community Hero
     assert new groovy.json.JsonSlurper()
      .parseText(messageExchange.responseContent)
      .find {it.key == "abc" && it.value == 123}
    • reyaz079's avatar
      reyaz079
      New Contributor

      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

    • AAB's avatar
      AAB
      Regular Contributor

      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....

       

       

      Thanks in advance for looking into this.

      Kind regards,

      AAB