首页 > 解决方案 > 如何在 SoapUI 中通过 groovy 检查请求值

问题描述

我有简单的模拟服务,请求如下:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fa1="http://TargetNamespace.com/FA1">
   <soapenv:Header/>
   <soapenv:Body>
      <fa1:items>
         <fa1:itemname>logo</fa1:itemname>
         <fa1:price></fa1:price>
         <fa1:vat>10</fa1:vat>
         <fa1:description>nové logo</fa1:description>
      </fa1:items>
   </soapenv:Body>
</soapenv:Envelope>

我使用脚本来控制响应值。一开始 XmlHolder 没有工作。所以我用

def req = mockRequest.getContentElement().execQuery("/")
def records = new XmlParser(false, false).parseText(req[0].xmlText())

获取请求并将其解析为节点。

{http://TargetNamespace.com/FA1}items[attributes={}; value=[
{http://TargetNamespace.com/FA1}itemname[attributes={}; value=[logo]], 
{http://TargetNamespace.com/FA1}price[attributes={}; value=[1]], 
{http://TargetNamespace.com/FA1}vat[attributes={}; value=[10]], 
{http://TargetNamespace.com/FA1}description[attributes={}; value=[nové logo]]]
]

但我无法获得价格价值。我努力了

def price = records.price.text()

表达式records.find( { it.text() == "1"} )返回{http://TargetNamespace.com/FA1}price[attributes={}; value=[1]]records.find( { it.name() == "price"} )同样records.find( { it.name() == "{http://TargetNamespace.com/FA1}price"} )返回 null。

标签: xmlgroovyrequestsoapui

解决方案


关键是用 def records = new XmlParser(假,假).parseText(req[0].xmlText())

这带来了这样的请求值:

fa1:items[attributes={xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/, xmlns:fa1=http://TargetNamespace.com/FA1}; value=[
fa1:itemname[attributes={}; value=[logo]], 
fa1:price[attributes={}; value=[1]], 
fa1:vat[attributes={}; value=[10]], 
fa1:description[attributes={}; value=[nové logo]]]
]

然后您可以按名称读取值,namespace:name因此 records.find( { it.name() == "fa1:price"} ).text()返回1


推荐阅读