How to assert on value in a Json response?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How to assert on value in a Json response?
Hi,
what would be the code to assert the value of "accesstoken" = " 1234" in Soap UI?
Which of the available Assertions do I need to pick (I guess XPath match, but not sure)?
and what would be the Regex?
Thanks for the help.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
$.accessToken should equal to the desired value.
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You may find some sample scripts below:
https://github.com/nmrao/soapUIGroovyScripts/tree/master/groovy
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@nmrao wrote:
For the mentioned case, JsonPatMatch is fine.
$.accessToken should equal to the desired value.
@nmraoI entered this (see screenshot), but it's not working. as you can see there is an accesstoken in the response, but validating it gives an error (it thinks it should be false?). Do you have any tips?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
http://jsonpath.com/
Following Script Assertion should help:
def parsedJson = new groovy.json.JsonSlurper().parseText(context.response)
log.info parsedJson.accessToken
//replace expectedValue below
assert expectedValue == parsedJson.accessToken, 'Not matched.'
You can also check here:
http://groovyconsole.appspot.com/script/5179916383420416
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@mgroen2, you're using JsonPath Regex Match. Try the regular JsonPath Match instead.
Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
@HKosova as you suggested, I implemented the JsonPath Match,
the strange this is, that validation initially succeeds (I clicked on "select from current"), however when re-executing the test it fails, and the reason for that is that expected value is changed, a comma is added to the end (see second screenshot).
After re-selecting the value again (" select from current"), the assertion passes again first time, but re-executing the test, makes the assertion fail again (comma is added again). Is this known behaviour? Possibly a bug?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Mathijs,
The comma is part of the error message, not the value. And the error happens because the token value is different - e.g. on your image the expected value ends with "...D8LE" and the actual value ends with "... JSPPA" (or vice versa).
I guess the token is generated dynamically and is different for each new session, like a unique session ID. How are you supposed to validate it? Do you just need to verify this token is present in the response, regardless of the value?
Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
