Forum Discussion

SmartBear217's avatar
SmartBear217
Contributor
5 years ago
Solved

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?

  • 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>"
    }

     

6 Replies

  • richie's avatar
    richie
    Community Hero
    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
      • richie's avatar
        richie
        Community Hero

        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