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.
Does anyone have an idea please?
Response is Json and looks like this
You won't be able to test the endpoint as there is a strong security on it.
Thanks in advance
Kind Regards,
AboveAndBeyond
Solved! Go to Solution.
Hello,
I've looked into this with a developper. For our response we see that we actually have an array in an array. Therefore the code needed to be adapted a bit.
The response looks like this:
Tue Sep 22 16:30:58 CEST 2020: INFO: [[nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], ......etc..]
Assertion second part:
.....
def checkForLanguage = "nl"
def onlyExpectedLanguage = true
language.each {
element -> element.each {
languageUnderTest -> {
if (!languageUnderTest.equals(checkForLanguage)) {
onlyExpectedLanguage = false
log.info "Found unexpected language: ${languageUnderTest}"
}
}
}
}
assert onlyExpectedLanguage, "Found a different language than ${checkForLanguage}"
In this case I was, with the previous code, asking to compare a string (def checkForLanguage = "nl") with an array ( def language = json.pdcData.concepts.description.language ) and thus of course the code passed each time because it was comparing something (array) with nothing (string).
So in order to be able to compare, I needed to loop over each array in that array.
Mostly the code is written like this:
def expected = true
x.each{
y -> y.each{
variableName -> {
//your statement here
}
}
}
assert expected, "error message here"
Thanks for your help anyways 😄
Kind regards,
AboveAndBeyond
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
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
Hello @richie
Thanks for the code and your time. it works as a charm for me
Thanks,
Kr,
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
Hi man, it seems that I was a bit to zealous
... 😞 it worked, .... at first glance, but when I fill in the value I'm expecting, it should fail the assertion and the code doesn't.
So.. @nmrao : any ideas please?
Context:
send request and receive only values in (here) english (= "en"). If other languages present then fail. If language present, pass.
In this case I started with ['fr', 'nl', 'de'] and it passed. The code doesn't find any of these apparently, but when I fill in 'en' it also passes...?
Thanks in advance.
AboveAndBeyond
@AboveAndBeyond could you let me know if you are still on the older ReadyAPI version?
I recommend updating to 3.2.2 and checking how it behaves there! The tool is getting a lot of improvements and fixes with each update, so let's see if this helps.
@AboveAndBeyond : Try with
assert checkforEN not in language : "Your message"
assert checkforNL not in language : "Your message"
assert checkforDE not in language : "Your message"
Hope this will help you out 🙂
Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓