Script Assertion
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 🙂
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Yes, I have changed the assert statement to meet my requirement. Thanks.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Regards,
Rao.
