cancel
Showing results for 
Search instead for 
Did you mean: 

How do I verify that multiple values are all the same for an unknown amount of values?

SOLVED
Contributor

How do I verify that multiple values are all the same for an unknown amount of values?

I have some XML and want to verify the name column all contain the same name. There may be 1 row or there may be 20 rows. There is no way to know how many there will be.


How do I verify the name of all of them is "Joe"?

 

Right now the Xpath is returning: [[ Joe , Joe , Joe , Joe , Joe , Joe , Joe , Joe , Joe , Joe , Joe , Joe , Joe , Joe , Joe , Joe , Joe , Joe , Joe , Joe ]]

 

But I can't just put "Joe" in the expected result because it's expecting more than one sometimes. So how do I make it dynamic so that it checks for "Joe" for any number of values?

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Contributor

Re: How do I verify that multiple values are all the same for an unknown amount of values?

Absolutely. Here's the way I did it in a groovy script assertion. First I retrieved the XML into an array and then looped through the array to verify each one. And that seems to be the problem with a regular Xpath assertion which is that I couldn't figure out how to loop through an unknown amount of values.

 

import com.eviware.soapui.support.XmlHolder

def holder = new XmlHolder(messageExchange.responseContentAsXml)

 

def resultsArray = holder.getNodeValues("<XPATH GOES HERE>")

 

//Loop array and verify what you need
resultsArray .each{
assert "${it}" == "<insert assertion string>"
}

 

View solution in original post

6 REPLIES 6
Highlighted
Community Hero

Re: How do I verify that multiple values are all the same for an unknown amount of values?

Hey @SmartBear217,

I have a script assertion (courtesy of @nmrao) that will do exactly what you need (works for .json, i'll need to chsnge it to work for .xml) on my laptop.

I'm typing this out on my phone, but once i'm in front of my laptop i'll update this post with the assertion's groovy script

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
Highlighted
Contributor

Re: How do I verify that multiple values are all the same for an unknown amount of values?

Awesome thanks! I appreciate the help!!

Highlighted
Community Hero

Re: How do I verify that multiple values are all the same for an unknown amount of values?

Hey @SmartBear217 

 

the json version is as follows

def json = new groovy.json.JsonSlurper().parseText(context.response) 
assert json.data.EffectiveTo.every{'2019-02-28 00:00:00.000' == it} 

//assertion verifies all EffectiveTo attributes in the json are '2019-02-28 00:00:00.000'. 
// JsonPath of EffectiveTo attribute is data.EffectiveTo

I just need to do a bit of reading to ensure XPath in groovy uses the dot notation and need to change the above to use XMLSlurper, but the above gives you the pattern of what you need.

 

I'll update this post once I've found the XML version of the above

 

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
Highlighted
Contributor

Re: How do I verify that multiple values are all the same for an unknown amount of values?

Ok thanks!

 

I actually found a way to do it using the XML holder. Using that you can get an array of values based on xpath and then loop through them all and verify.

Highlighted
Community Hero

Re: How do I verify that multiple values are all the same for an unknown amount of values?

Hey @SmartBear217,

Great! Smiley Happy

Yep XMLHolder() will do it....i considered that instead of using XMLSlurper().

Can you please publish your code so other people can benefit from it please?

Ta

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
Highlighted
Contributor

Re: How do I verify that multiple values are all the same for an unknown amount of values?

Absolutely. Here's the way I did it in a groovy script assertion. First I retrieved the XML into an array and then looped through the array to verify each one. And that seems to be the problem with a regular Xpath assertion which is that I couldn't figure out how to loop through an unknown amount of values.

 

import com.eviware.soapui.support.XmlHolder

def holder = new XmlHolder(messageExchange.responseContentAsXml)

 

def resultsArray = holder.getNodeValues("<XPATH GOES HERE>")

 

//Loop array and verify what you need
resultsArray .each{
assert "${it}" == "<insert assertion string>"
}

 

View solution in original post