soapUI(groovy脚本作用2)请不要问为什么系列2
在构造用例过程中可能需要对返回结果进行校验,此时可以借助Groovy脚本完成此功能,具体步骤如下:
(1)通过SoapUI提供的GroovyUtils获取返回的xml消息的操作XmlHolder
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( "balanceQuery#Response" )
(2)在XmlHolder中使用Xpath获取返回字段内容
log.info(holder.getNodeValue("//uvs:balanceQuery/uvs:BalanceQueryRequest/uvs:RequestMessage/uvs:MessageBody/uvs:SubscriberNo"));
def messageBody =
holder.getDomNode("//uvs:balanceQuery/uvs:BalanceQueryRequest/uvs:RequestMessage/uvs:MessageBody")
log.info(messageBody.getNodeValue())
def subscriberNo =
messageBody.getElementsByTagName_r("SubscriberNo");
log.info(subscriberNo)
//获得节点对象的xml
//log.info(holder.xml)
(3)如需通过xmlHolder获取其余信息参考如下:
GroovyUtils 与 XmlHolder 参考:
GroovyUtils currently includes the following (few) methods:
·
projectPath
: a property holding the path to the containing
project, useful for accessing data files in same folder
·
setPropertyValue( String testStepName, String propertyName, String
value )
: sets the specified property value
·
expand( string )
- expands the specified Property
Expansion string
·
getXmlHolder( String xmlPropertyOrString )
: Creates an
XmlHolder
object (see below) for easily accessing/modifying
contents of an XML document using XPath expressions. The argument
must either be a TestStep property in the
TestStepName#PropertyName
format or a valid XML string
XmlHolder object has the following methods:
·
getNodeValue( String xpath )
: returns the value of the first node pointed to
by the specified XPath expression_r(can be replaced by holder[xpath]
expression, see below )
·
getNodeValues( String xpath )
: returns a String array containing the values of
all nodes pointed to by the specified XPath expression.
·
getDomNode( String xpath )
: returns the DOM Node of the first node pointed
to by the specified XPath expression.
·
getDomNodes( String xpath )
: returns a DOM Node array containing all nodes
pointed to by the specified XPath expression.
·
setNodeValue( String xpath, String value )
: sets the content of the first node pointed to
by the specified XPath expression to the specified value (can be
replaced by holder[xpath] = value expression, see below )
·
declareNamespace( String prefix, String namespaceURI )
: declares a namespace that will be used in a
following get/set operation, can also be set with
holder.namespaces[prefix] = namespaceUri (see example below)
·
getNamespaces()
- returns a Map of prefixes to namespace URI:s
that will be used in XPath expressions
·
removeDomNodes( xpath )
- removes all DOM nodes matching the specified
XPath expression
·
xml
: property containing the updated xml string
·
xmlObject
: property containing the parsed XMLBeans
XmlObject for the xml string
·
prettyXml
: property containing the pretty-printed updated
xml string
·
updateProperty()
: if the XmlHolder was created from a TestStep
property, that property will be updated with the currently held xml
(see example below)
·
updateProperty( boolean prettyPrint )
: same as previous, with option to pretty print
the updated xml. Defaults to false when not specified.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述