cancel
Showing results for 
Search instead for 
Did you mean: 

Not Contains Assertion (JSON attribute value) - not XML

SOLVED
Community Hero

Re: Not Contains Assertion (JSON attribute value) - not XML

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

Re: Not Contains Assertion (JSON attribute value) - not XML

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

Re: Not Contains Assertion (JSON attribute value) - not XML

@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.
Community Hero

Re: Not Contains Assertion (JSON attribute value) - not XML

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 title of the post to something more descriptive? This will help people when searching for problems.
Highlighted
Community Hero

Re: Not Contains Assertion (JSON attribute value) - not XML

@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.
New Here?
Join us and watch the welcome video:
Top Kudoed Authors