AutoTig AutoTig -4 years ago 205
Groovy Question

Counting response nodes in soapUI with groovy

I am trying to count response nodes and then do something over the amount of nodes.
I've tried it in the following groovy script but the count remains "0":

def collection = ["1","2","3","4"]

// get num repetitions of <SubsPlanDto> with Xpath count
def numElements = context.expand( '${QuerySubsPlanList - Request 1#Response#//count(*:QuerySubsPlanListResponse[1]/SubsPlanDtoList[1]/SubsPlanDto)}')
def collection2 = [];
// iterate and add the values to a collection
for ( i in 1..numElements.toInteger()) {
collection2.add(context.expand( '${QuerySubsPlanList - Request 1#Response#//*:QuerySubsPlanListResponse[1]/SubsPlanDtoList[1]/SubsPlanDto['+ i + ']/SubsPlanCode[1]}'))
}

// get the matches and differences
def matches = collection.intersect(collection2)
def fails = collection.plus(collection2)
fails.removeAll(matches)

log.info matches
log.info fails


The structure of the response is as follows

xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:QuerySubsPlanListResponse xmlns:ns="http://com.ztesoft.zsmart/xsd">
<SubsPlanDtoList>
<SubsPlanDto>
<SubsPlanCode>TEST1</SubsPlanCode>
<ServType>M</ServType>
<CustTypeList>B2B</CustTypeList>
<Price>0.0000</Price>
</SubsPlanDto>
<SubsPlanDto>
<SubsPlanCode>TEST2 </SubsPlanCode>
<ServType>M</ServType>
<CustTypeList>B2C|B2B</CustTypeList>
<Price>0.0000</Price>
</SubsPlanDto>


Can anyone tell me how to count the nodes correctly?
Thanks

Answer Source

context.expand() returns a String! Also, your XPath has multiple problems. You probably want:

def numElements = context.expand('${QuerySubsPlanList - Request 1#Response#count(//*:SubsPlanDto)}').toInteger()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download