cancel
Showing results for 
Search instead for 
Did you mean: 

Script Assertion

SOLVED
NinjaMarvel
New Contributor

Script Assertion

I am using ReadyAPI and I am getting following response from API in JSON.

{
"message" : "IDs found for the given request",
"ids" : [
{
"id" : 3831740,
"Member" : false
},
{
"id" : 5088068,
"Member" : false
},
{
"id" : 17424086,
"Member" : true
},
{
"id" : 17425459,
"Member" : true
}
]
}

I need to add assertion to find count of the "Member" for true and false separately.

Exactly assertion required:
Find count of "Member" : true
Find count of "Member" : false

1 ACCEPTED SOLUTION

Accepted Solutions
nmrao
Community Hero

Re: Script Assertion

Here you go:

The script which can be tested online(note that it is using fixed json):
https://ideone.com/6oaYhn

You need modify to use dynamic json in script assertion.


Regards,
Rao.

View solution in original post

8 REPLIES 8
richie
Community Hero

Re: Script Assertion

Hi,

 

I'm also very interested in this - I've never had to do it at all - but it seems like it would pop up sooner or later.

 

I did have a go at this myself - i knew it was a bit beyond me - but I thought I might be able to work it out with a bit of reading - but what I read said that the groovy count function doesnt appear to work with strings - it works with maps, arrays and collections - but when I get a json response I think I normally handle it as a string? so I dont know how to get it to work

 

so - apologies - I know this post isn't helpful at all - but at least I can show you what I've got so far

 

def json = new groovy.json.JsonSlurper().parseText(context.response) // its a string at this point - right?
assert json.Member.every{it == false || it = true}

//the above would assert that the repeating Member attribute displays either 'false' or 'true'

I found the following on groovy goodness site

def list = ['Groovy', 'Grails', 'Java']
assert list.count { it.startsWith('G') } == 2
 
def numbers = [1,2,3,4] as Integer[]
assert numbers.count { it > 2 } == 2
 
def map = [user: 'mrhaki', city: 'Tilburg', age: 37]
assert map.count { key, value -> key.size() == 3 } == 1

so I was trying to edit my original script using this - but I was unsuccessful

 

I updated my original script as follows:

def json = new groovy.json.JsonSlurper().parseText(context.response) 
assert json.Member.count{it == false || it = true} // the script passed - but if Member contains other values <> false or true - the assertion still passes

 

but as I mention in the comment - this doesnt do what I need either!

 

Would welcome any hints/tips to steer me in the right direction!

 

As always - thanks to all!

 

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
NinjaMarvel
New Contributor

Re: Script Assertion

Hi Rich...Thank you very much for trying to find the solution. Even I'll keep trying and if someone knows the solution to my problem, plz help me out. Thanks in advance 🙂

richie
Community Hero

Re: Script Assertion

Hey @NinjaMarvel 

 

I'm not a groovyscripter - I just play around cos I like it - but I'm surprised one of the coders hasn't come back to provide an answer for you yet - cos normally they are pretty quick off the mark and very, very helpful.

 

I'd suggest they're probably on holiday at the moment.  I'll start playing with it again - I might stumble across what I'm doing wrong if I do some more reading (I actually stopped looking cos I'd spent a couple of hours trying to get it to work and I suspected one of the coders would answer within 24hours with a working answer anyway).

 

I'm positive the coders can answer this question - it just might take a couple more days before you get a response.

 

Cheers man,

 

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

Re: Script Assertion

Here you go:

The script which can be tested online(note that it is using fixed json):
https://ideone.com/6oaYhn

You need modify to use dynamic json in script assertion.


Regards,
Rao.

View solution in original post

NinjaMarvel
New Contributor

Re: Script Assertion

Hi Rao,

Thank you very much for the solution. It's working.

 

And just wanted to know how to set the expectation if trueCount or falseCount is greater than (>) or lesser than (< ) some value. for e.g. If I need to define the count of true > 2

 

Thanks in advance 🙂

 

Regards,

NinjaMarvel

nmrao
Community Hero

Re: Script Assertion

You wouldn't know the expected count before the test? You can play with assert statement the way you needed.


Regards,
Rao.
NinjaMarvel
New Contributor

Re: Script Assertion

Yes, I have changed the assert statement to meet my requirement. Thanks.

nmrao
Community Hero

Re: Script Assertion

That is nice hear. Glad to know.


Regards,
Rao.
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors