使用groovy计算soapUI中的响应节点

时间:2014-08-05 11:38:04

标签: groovy soapui

我正在尝试计算响应节点,然后在节点数量上做一些事情。 我已经在下面的groovy脚本中尝试了它,但计数仍为“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

响应的结构如下

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> 

有谁能告诉我如何正确计算节点? 感谢

1 个答案:

答案 0 :(得分:1)

context.expand()会返回字符串!此外,您的XPath有多个问题。你可能想要:

def numElements = context.expand('${QuerySubsPlanList - Request 1#Response#count(//*:SubsPlanDto)}').toInteger()