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.

原文地址:https://www.cnblogs.com/killbug/p/2458688.html