xml - 如何在 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。
解决方案
关键是用
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
推荐阅读
- javascript - 查找总和为给定数量数组发行的最小数量的纸币和价值
- windows - 为什么连续调用 SetForegroundWindow 不起作用?
- java - 我应该使用哪种图形算法来找到通过所有给定目的地的最快路线?
- c - 了解 SISL 库中的原型/函数语法
- python-3.x - 你如何让 pytest 像 python 一样理解 python 模块?
- javascript - 在 vuejs 中,输入事件触发在列表中不起作用
- azure-cosmosdb - 如何使用 pymongo 获取 CosmosDB 查询中消耗的 RU/s?
- python - 绘制 3D 曲面图,其中颜色条代表另一个数量
- homebrew - 将 OSX 升级到 Catalina 后出现 zsh 问题
- excel - Excel 将存储为文本 YYYYMMDDhhmmss 的 DateTime 转换为格式化的日期和时间“mm/dd/yy hh:mm”