cancel
Showing results for 
Search instead for 
Did you mean: 

iterate over a specific node in web service response using groovy

MKV
Contributor

iterate over a specific node in web service response using groovy

Hi,

I want to iterate over a specific node in web service response and store the data in an array. I tried to do some groovy scripting referring the samples in the forum. But the data doesn't get added to the array. I'm trying to loop over the attributeName node in the below sample. Kindly help to resole the issue.

 

My response is like this:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns4:GetAccountDetailsResponseMessage>

</ns4:account>

.....

<ns6:CustomCustomerAccount>

<ns3:attributes>
<ns3:attributeName>TELNUM</ns3:attributeName>
<ns3:attributeId>4232323</ns3:attributeId>
<ns3:attributeValue/>
</ns3:attributes>

<ns3:attributes>
<ns3:attributeName>SMSNUM</ns3:attributeName>
<ns3:attributeId>6565464</ns3:attributeId>
<ns3:attributeValue/>
</ns3:attributes>

</ns6:CustomCustomerAccount>
</ns4:account>
</ns4:GetAccountDetailsResponseMessage>
</soap:Body>
</soap:Envelope>

 

My Groovy scripting is as below:

import groovy.util.XmlSlurper
import com.eviware.soapui.support.XmlHolder

def WS_Data = context.expand( '${WS_GetAccountDetails#Response#declare namespace ns6.....GetAccountDetailsResponseMessage[1]/ns4:account[1]/ns6:CustomCustomerAccount[1]}' )

log.info("Start")
class AttributesInfo {
def attributeNameObj
}
List<AttributesInfo> AttributesInfoList = new ArrayList<>()
def WS_Results = new XmlSlurper().parseText(WS_Data)
def WS_attributes = WS_Results.Body.GetAccountDetailsResponseMessage.account.CustomCustomerAccount.attributes

// iterate over attributes data
WS_attributes.each { attributes ->
AttributesInfo attributeInfo = new AttributesInfo()
attributeInfo.attributeNameObj = attributes.attributeName
AttributesInfoList << attributeInfo
}
log.info (AttributesInfoList)

log.info("End")

 

Thanks!!

8 REPLIES 8
msiadak
Community Hero

Re: iterate over a specific node in web service response using groovy

Am I corect in my assumption that you are trying to parse one XML object and all of its elements to use them in future requests? Groovy will work, but it may take a lot of different steps. You might want to look at using an XML data source. That might better fit what you want. You can set it to parse an XML response and use XPATH to select elements and load them into properties. 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
MKV
Contributor

Re: iterate over a specific node in web service response using groovy

Hi,

 

I tried to use it as XML Datasource. I created the properties and executed it. But the data log doesn't seem to be displaying any data although it says Got 5 rows in 3ms.

 

1. Is there something that needs to be done, apart from providing the row Xpath, defining property?

2. Can i use the output data values to get stored in an array?

 

Please suggest.

msiadak
Community Hero

Re: iterate over a specific node in web service response using groovy

Can you share some details of what you have tried? Either your project or screen shots? 

What I have done in the past is similar to this, as a high level view:

  1. Make a Read request that returns one object.
  2. Set up an XML datasource that uses XPATH to point to properties in the response from step 1.
    1. Example properties:
      1. UserName
      2. TimeStamp
      3. Identifier
    2. If you manually run the DataSource, you should see the data it returns in the data log window. If you do not see data, it is not configured correctly.
  3. Set up furthe Read requests that use data from the XML Data Source
    1. Test steps would reference the data as ${XMLDataSource#PropertyName}
      1. ${XMLDataSource#UserName}
      2. ${XMLDataSource#TimeStamp}
      3. ${XMLDataSource#Identifier}
  4. An example test case would look like
    1. Read request to populate XML DataSource
    2. XML DataSource
    3. ReadRequest using Property1
    4. ReadRequest using Property2
    5. ReadRequest using Property3
    6. etc.

 

Hope that helps!




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
MKV
Contributor

Re: iterate over a specific node in web service response using groovy

You are correct. I did a mistake in the configuration. Using one of your earlier comment in a post (https://community.smartbear.com/t5/SoapUI-Pro/DataSource-XML-not-returning-data/td-p/147087/page/3), I corrected it and now the data is getting displayed.

 

Now, I want to pass this data into an array. Can you please help to do that as well.

 

msiadak
Community Hero

Re: iterate over a specific node in web service response using groovy

Glad you got the data getting returned. It took me awhile to puzzle that piece out my first time. 🙂 When you say store these pieces of data in an array, what do you mean and what is the intent? I may need some examples of what you are trying to do and some sample requests / responses / etc to figure this next part out. 🙂 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
MKV
Contributor

Re: iterate over a specific node in web service response using groovy

I have a set of attributes in response which is stored in DB in JSON format. Validating if the details in DB are retrieved in response correctly is my final motive.

 

So, I have split the JSON data and stored it in different arrays - say attributeName, attributeId, attributeValue.

Similarly, I wanted to store the xml response attribute details in an array and finally compare both the arrays.

 

I have successfully did the Json data storing in array. Now, I wanted to have the response data also to be stored in array, so that i can conclude by comparing both the arrays. Please suggest.

 

Sample response:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns4:GetAccountDetailsResponseMessage>

</ns4:account>

.....

<ns6:CustomCustomerAccount>

<ns3:attributes>
<ns3:attributeName>TELE</ns3:attributeName>
<ns3:attributeId>TRUE</ns3:attributeId>
<ns3:attributeValue/>
</ns3:attributes>

<ns3:attributes>
<ns3:attributeName>SMS</ns3:attributeName>
<ns3:attributeId>AVAILABLE</ns3:attributeId>
<ns3:attributeValue/>
</ns3:attributes>

</ns6:CustomCustomerAccount>
</ns4:account>
</ns4:GetAccountDetailsResponseMessage>
</soap:Body>
</soap:Envelope>

 

MKV
Contributor

Re: iterate over a specific node in web service response using groovy

I was trying to iterate specific xml node. I could get only the first node value only. Not able to iterate. Please help.

 

This is my script:

def WS_attributeNameList=[]
def WS_attributeNameList_Converted=[]

 

import com.eviware.soapui.support.XmlHolder
def response = context.expand( '${WS_GetAccountDetails#Response#declare namespace ....//ns4:GetAccountDetailsResponseMessage[1]}' )
def xml = new XmlHolder(response)
WS_attributeNameList = context.expand( '${WS_GetAccountDetails#Response#declare namespace ....//ns4:GetAccountDetailsResponseMessage/ns4:accounter/ns6:CustomerCustomAccounter/ns3:attributes/ns3:attributeName}' )
WS_attributeNameList_Converted=WS_attributeNameList.toString()
log.info (WS_attributeNameList_Converted)

 

I'm not sure how to use groovy for iterating specific node - say attributename and store them in array. If I can do this, I can then sort it and make the required validation. Please help.

MKV
Contributor

Re: iterate over a specific node in web service response using groovy

I just realised to use Data sink option. I'm able to get the data source output values. But they are not getting appended to the array. Can someone help to append the data sink values to an array. [so it does not overwrite the existing values]

I have created a data sink with Groovy where my groovy script goes like this.

def WS_AttributeNameList = []

SinkValue=context.expand('${DataSink_AttributeName#WS_AttributeName}')

WS_AttributeNameList.add(SinkValue)

log.info (WS_AttributeNameList)

 

Every time, it overwrites the value in the current iteration. It doesn't seem to append the data. Can someone please help to resolve this.

 

Thanks!

 

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors