SoapUI-如何从响应中断言

发布于 2024-10-13 07:59:23 字数 1668 浏览 4 评论 0原文

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

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>

I am new to SoapUI I have a basic question. When i get a response to a services i would like to do an assert for a value. below is scripts i had created under 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

儭儭莪哋寶赑 2024-10-20 07:59:23

能够找到解决方案。命名空间在使用 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>

Was able to find the solution. Namespace is decalared before using 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文