0

I'm making an automated test suite for an API with an odd schema. In my API response, I have a list of types of items people can order and some information about them like price. Each item has an ID:

<xml> 
<ItemType ID="Type1">
<Price>5.00</Price>
</ItemType>

<ItemType ID="Type2">
<Price>10.00</Price>
</ItemType>
</xml>

Later in the API there is a list of actual products. Each one has an ItemID so that you can find out how much it costs, like this:

<Product>
<Name>Product1</Name>
<Description>some stuff</Description>
<ItemRefs>Type1</ItemRefs>
</Product>

I select a product and save its ItemRefs as a property to use in the next API. However, I also need to work out the price of the product I chose. I currently always select the first product in the list but this doesn't always correlate to the first type of item. I also want to be able to make tests selecting multiple products and types of items in the future, but I would need to find the price of all of them.

I need a way to find my ItemRefs property within the earlier list and set the associated price as another property. I'm assuming I need to do this with a groovy script but I don't know how. Can anyone help?

Gaurav Khurana
  • 3,423
  • 2
  • 29
  • 38

1 Answers1

0

So i have taken a request whose name is NameofTherequest and assuming your XML is placed in the response

So first we are getting the response in holder and then getting the values you want

So first of all we get all Ids in an Array and then corresponding to each Id we get the price and store in hashmap. Then whatever we need we can take it out from hashmap

 def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
 def holder = groovyUtils.getXmlHolder("NameofTherequest#Response")

  // Take out the values of ids and their corresponding price
   def Ids = holder.getNodeValues("//*:ItemType/@ID")
   log.info "The values of all Ids are  = " + Ids.toString()
   def Price = holder.getNodeValues("//*:ItemType/*:Price")
   log.info "The values of prices are  = " + Price.toString()


   // A better logic where we store each Price against its ID value in a      hashmap
    HashMap h=[:]
   for(def var in Ids)
   {
   def Price1 = holder.getNodeValue("//*:ItemType[@ID='${var}']/*:Price")
    h.put(var,Price1)   
    }

    log.info "The values of prices are in hashmap = " + h

  // Now suppose you want to know the price of say Type2
   def str="Type2" // You can get this from property value whose price you want to know

    log.info "The price of $str is " + h[str]

The output of the above code is

enter image description here

Gaurav Khurana
  • 3,423
  • 2
  • 29
  • 38