SoapUI-如何从响应中断言

时间:2011-01-21 09:40:27

标签: assert soapui

我是SoapUI的新手我有一个基本的问题。当我得到对服务的响应时,我想对一个值进行断言。下面是我在Script Assertion下创建的脚本

import com.eviware.soapui.support.XmlHolder

def holder = new XmlHolder(messageExchange.responseContentAsXml)
assert holder["//ConstraintId[0]"] =="5000006";

I get the following error:
assert holder["//ConstraintId[0]"] =="5000006" | | | | [] false com.eviware.soapui.support.XmlHolder@8b6500 (toString() threw java.lang.NullPointerException)

**********************************************************
import org.xml.sax.helpers.DefaultHandler

def rootNode = new XmlSlurper().parseText(messageExchange.responseContentAsXml)
assert rootNode.Body.Constraintid[0].text=="5000006";

I get the following error:
assert rootNode.Body.Constraintid[0].text=="5000006" | | | | | | | | | | | false | | | | groovy.util.slurpersupport.NodeChildren@17a5c01 (toString() == "") | | | groovy.util.slurpersupport.NoChildren@2e80e1 (toString() == "") | | groovy.util.slurpersupport.NodeChildren@10f682f (toString() == "") | 

Response
   <soap:Body>
      <GetEnumResponse xmlns="http://www.xyz.com/">
         <GetEnumResult>
            <ErrorCode>0</ErrorCode>
            <StatusId>0</StatusId>
         </GetEnumResult>
         <enumsInformation>
            <EnumInformation>
               <TransactionId>0</TransactionId>
               <ConstraintId>5000006</ConstraintId>
               <EnumValue>abc</EnumValue>
               <Index>10</Index>
            </EnumInformation>
         </enumsInformation>
      </GetEnumResponse>
   </soap:Body>

1 个答案:

答案 0 :(得分:2)

能够找到解决方案。在使用getNodeValue之前,命名空间被解除。

import com.eviware.soapui.support.XmlHolder
def holder = new XmlHolder(messageExchange.responseContentAsXml)

holder.namespaces["tal"]="http://www.xyz.com/"
def node = holder.getNodeValue("//tal:ConstraintId[1]");
log.info(node);
assert node == "5000006";

<soap:Body>
      <GetEnumResponse xmlns="http://www.xyz.com/">
         <GetEnumResult>
            <ErrorCode>0</ErrorCode>
            <StatusId>0</StatusId>
         </GetEnumResult>
         <enumsInformation>
            <EnumInformation>
               <TransactionId>0</TransactionId>
               <ConstraintId>5000006</ConstraintId>
               <EnumValue>xyz</EnumValue>
               <Index>10</Index>
            </EnumInformation>
         </enumsInformation>
      </GetEnumResponse>
   </soap:Body>