使用groovy更新soap请求(节点值和属性值)

时间:2017-07-21 10:38:30

标签: groovy soapui

我尝试使用groovy更新soap消息。节点值可以使用以下脚本更新。有人帮我更新属性值。

XML:

<TITLE>Computer Parts</TITLE>
<PART Price="High">
   <ITEM>Motherboard</ITEM>
   <MANUFACTURER>ASUS</MANUFACTURER>
   <MODEL>P3B-F</MODEL>
   <COST> 123.00</COST>
</PART>

更新节点 xpath = //*:PART/*:COST/

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( "SOAP_MSG#Request" )

holder.setNodeValue( xpath, "200" )

holder.updateProperty()

更新属性 xpath = //*:PART/@Price

如何更新Price属性?

1 个答案:

答案 0 :(得分:0)

这是脚本,并在线查找注释:

//Pass xml string to parseText method
def pxml = new XmlSlurper().parseText(xml)

//Have the expected new cost
def expectedCost = 200.00

//Have the expected new price
def expectedPrice = 'Low'

//Get the cost node
def cost = pxml.'**'.find{it.name() == 'COST'}

//Replace the value with expected value
cost.replaceBody(expectedCost)

//Get the cost node
def part = pxml.'**'.find{it.name() == 'PART'}

//Replace the value with expected value
part.@Price = expectedPrice

//Print the updated xml; use log.info if using in soapui
println groovy.xml.XmlUtil.serialize(pxml)

在线 Demo