加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

SoapUI中使用Groovy修改请求消息&校验返回消息

发布时间:2020-12-14 17:03:05 所属栏目:大数据 来源:网络整理
导读:SoapUI在测试WebService消息时,在构造用例过程中部分请求参数可能需要通过查询数据库或者通过随机数获取,此时可以借助Groovy脚本完成此功能,具体步骤如下: (1)从TestCase中获取请求消息 def request = testRunner.testCase.getTestStepByName( "TestStep

SoapUI在测试WebService消息时,在构造用例过程中部分请求参数可能需要通过查询数据库或者通过随机数获取,此时可以借助Groovy脚本完成此功能,具体步骤如下:

(1)从TestCase中获取请求消息

def request = testRunner.testCase.getTestStepByName( "TestStep名称" );

def property = request.getProperty( "request" );

log.info(property.value)

(2)将请求消息转换为String,并通过XmlParser对其进行修改

def balanceQueryParser = new groovy.util.XmlParser(false,false).parseText(property.value);

//获取需要修改的Node节点,每个节点都需要指定前缀限定,通过此种方式获取的节点为NodeList,所以需要加上[0]
def transactionid = balanceQueryParser["soapenv:Body"]["uvs:balanceQuery"]["uvs:BalanceQueryRequest"]["uvs:RequestMessage"]["uvs:MessageHeader"]["uvs:TransactionId"][0];

//通过Node.setValue方法可以对请求参数进行修改
transactionid.setValue(System.currentTimeMillis())

(3)将请求消息写回到TestStep中
def writer = new java.io.StringWriter();
def printer = new groovy.util.XmlNodePrinter( new PrintWriter( writer ));
printer.print( balanceQueryParser );
property.setValue( writer.toString() )
log.info(property.value)

在构造用例过程中可能需要对返回结果进行校验,此时可以借助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("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 (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.

?

PS: http://blog.csdn.net/shuaihonggao/archive/2010/09/06/5866845.aspx

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读