Forum Discussion

chathurad's avatar
chathurad
Contributor
6 years ago

Assert whether all the nodes containing the specific text

For a get API im pasing country as a parameter,And the return json contains several nodes with country tag.

I need to assert whether all the nodes contaning country which i pass at the inital API call.

 

Im passing country ass New Zealand and as on the below output json contains several result sets with the country new zeland.

 

Hope some one can help me with this.

   "result":    [
            {
         "minStudentAge": 16,
        
         "country": "New Zealand",
         "name": "Auckland Institute of Studies",
         "language": ["English"],
         "videoUrl": "https://www.youtube.com/embed/KHojs7-xWUM",
         "city": "Auckland",
         "activities":          [
            "beach",
            "bungy",
            "caves",
            "cycling",
            "hiking",
            "hobbiton",
            "horse-riding",
            "maori-culture",
            "museums",
            "nightlife",
            "skydiving",
            "surfing-and-kitesurfing",
            "thermal-hot-pools",
            "waterfalls"
         ],
         
         "country": "New Zealand",
         "name": "Dominion English Schools",
         "language": ["English"],
         "videoUrl": "https://www.youtube.com/embed/FbJM588VsPA",
         "city": "Auckland",
         "activities":          [
            "beach",
            "bungy",
            "caves",
            "cycling",
            "hiking",
            "hobbiton",
            "horse-riding",
            "maori-culture",
            "museums",
            "nightlife",
            "skydiving",
            "surfing-and-kitesurfing",
            "thermal-hot-pools",
            "waterfalls"
			"country": "New Zealand",
		],
         "name": "Dominion English Schools",
         "language": ["English"],
         "videoUrl": "https://www.youtube.com/embed/FbJM588VsPA",
         "city": "Auckland",
         "activities":          [
            "beach",
            "bungy",
            "caves",
            "cycling",
            "hiking",
            "hobbiton",
            "horse-riding",
            "maori-culture",
            "museums",
            "nightlife",
            "skydiving",
            "surfing-and-kitesurfing",
            "thermal-hot-pools",
            "waterfalls"
         ],

7 Replies

  • i came up with this 

     

    import com.eviware.soapui.support.XmlHolder
    import net.sf.*;
    import net.sf.json.*;
    import net.sf.json.groovy.*;
    
    def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
    
    //Getting response from methods
    def response = context.expand( '${Validate valid responce code#Response}' ).toString()
    
    def slurper = new JsonSlurper()
    def json = slurper.parseText response
    def countryList= json.result.country

    Now my 

    countryList 

    looks like an array.

    So now have to assert whether each elemant on countryList is equal to "New Zelan".

     

    But how to do that?

     

    • JHunt's avatar
      JHunt
      Community Hero

       

      countryList.each { assert it == "New Zealand" }

       

      • JHunt's avatar
        JHunt
        Community Hero

        Or...

         

        // ...
        def json = slurper.parseText response json.result.each { assert it.country == "New Zealand" }