Ask a Question

Not Contains Assertion (JSON attribute value) - not XML

SOLVED
richie
Community Hero

Not Contains Assertion (JSON attribute value) - not XML

Hey,

 

I've been investigating the Not Contains assertion to verify that a json attribute does NOT contain a specific value.

 

HOWEVER - all the posts I've seen on this on this forum/stackoverflow (stackoverflow post)etc. and soapuiforum either provide a groovyscript option to handle the not contains or uses XML as the example for the not contains.

 

HOWEVER - my response is in .json and my groovyscript is rubbish so I'd like to find the OTB solution.

 

I want to assert that a specific name/value pair attribute in my json response does NOT contain a certain value.

 

As stated above - I've seen the following Not Contains assertion value repeated in lots of places to assert that a tag doesnt contain the value zero, but this is XML, NOT json.

(?s).*<recordCount>[^0]</recordCount>(?s).*

Does anyone know the syntax to do this in jsonpath? 

 

I miss XPATH & XML....

 

thanks to all! πŸ™‚

 

richie 

 

 

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 Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
14 REPLIES 14
richie
Community Hero

Hey @rao

Im trying to extend the script so it checks multiple attributes rather than just the single item....the requirements got updated and so now i need to alter the script.

I'll hit you back with my attempt and you can rip it apart!

Cheers!

richie

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

Hi,

 

Low priority Query - I've found a way around this by adding in a separate groovy step instead - I was just wondering the reason for the behaviour

 

 

Original script is as below - and it asserts that the 'entityLogicalName' attribute value is <> 'appointment'  - courtesy of @rao

**
* Below is the script assertion for the REST Request Test step
**/
def json = new groovy.json.JsonSlurper().parseText(context.response)
def logicalNames = json.sessiondata.'appointment.nhs_slottype'.entityLogicalName
def checkFor = 'appointment'
//Negative check - value should not have appointment "!=", 
//Positive check -  use "==" to match value with appointment
assert logicalNames.every {it != checkFor}, "Not expecting value ${checkFor} for entityLogicalName, but found"

so - I decided due to project requirements that I needed to assert that a second attribute value should NOT contain a certain value - I duplicated the def logicalNames, def checkFor and the assertlogicalNames lines for an extra attribute check as below:

 

def json = new groovy.json.JsonSlurper().parseText(context.response)

def dataDescriptionvalue = json.data.Description
def checkForDescriptionval = 'A list of certificates'

def dataTitlevalue = json.data.Title def checkForTitleval = 'Certificates' //Negative check - value should not have certificates "!=", //Positive check - use "==" to match value with certificates
assert dataDescriptionvalue.every {it != checkForDescriptionval}, "Not expecting ${checkForDescriptionval} value for dataDescriptionvalue, but ${checkForDescriptionval} value was found" assert dataTitlevalue.every {it != checkForTitleval}, "Not expecting ${checkForTitleval} value for dataTitlevalue, but ${checkForTitleval} value was found"

HOWEVER - the second assertion never fires - essentially cos the step fails I suppose.  

 

Can anyone confirm that this is the case (I removed the 'abort test if error occurs' checkbox on the test case) - but the second assertion didn't appear to fire

 

I did get around this by adding splitting the code to check the 2nd attribute and the associated assertion into a second groovy step - so my test case hierarchy is as follows:

 

REST Step

GroovyStep - checks the 'Description' attribute and asserts it doesn't contain 'A list of certificates'

GroovyStep - checks the 'Title' attribute and asserts it doesn't contain 'Certificates'

 

I am just wondering if there's a reason why you can't have another assertion in the same groovy test step?  I understand it complicates things in case you get one assertion passing and one failing, but I would've thought it would report both the step failing anyway - just like it should in a manual test if you ever do include 2 assertions/verifications in the same step.....

 

Cheers!

 

richie

 

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

@richie ,

 

Couple of things.

 

1. The code snippet provided earlier is Script Assertion. And that can't into Groovy Step, and that does not work.

 

2. If the use case is to validate the response, assertion is the right way to check and fail the test step. Use of Groovy step is not correct thing.

 

3. Depends on the problem, solution varies. 

 

4. If an assertion fails, following code will not be executed at all. There is different way to deal with it.

 

5. Can't see the such data mentioned in the re-written script in earlier attached json.



Regards,
Rao.
richie
Community Hero

Hey @rao

 

my bad fella - you do actually state it's a script assertion i many, many places - but I wasn't paying attention!

 

Thanks man - yeah - works a lot better as a script assertion too! 

 

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

@richie 

Life is all about paying attention and learning!

No worries. So, can you come to main question and provide details if you are looking the solution? Of is this solved?



Regards,
Rao.
cancel
Showing results forΒ 
Search instead forΒ 
Did you mean:Β