在soapui groovy脚本中从xml获取文本值 [英] Get text value from xml in soapui groovy script

查看:191
本文介绍了在soapui groovy脚本中从xml获取文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是从soapui中的xml中获取值,以创建序列中的下一个测试。



我使用groovy脚本



我的目标是在标签内获取文本。很容易识别,因为它是base64编码的字符串。

  def project = testRunner.testCase.testSuite.project; 
def tcase = project.testSuites [ChangeRetentionByCopyDemoSuite]。testCases [Milestone2];
def tstep = tcase.getTestStepByName(getDocuments - GetContentURLRequest);

def responseTestSuite1 = tstep.getPropertyValue(response);

log.info(responseTestSuite1.toString());

def gutils = new com.eviware.soapui.support.GroovyUtils(context);
def holder = gutils.getXmlHolder($ responseTestSuite1);
$ b def byteResponse = holder.getNodeValue(/ S:Envelope / S:Body / g:getDocumentsResponse / text());

log.info(byteResponse);

xml在 getDocuments - GetContentURLRequest 中响应
$ b


 < S: Envelope xmlns:S =http://schemas.xmlsoap.org/soap/envelope/> 
< S:标题>
< Security xmlns =http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd>
< Timestamp xmlns =http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd>
<创建> 2015-09-15T13:57:50.844Z< /创建>
< Expires> 2015-09-16T13:57:50.844Z< / Expires>
< /时间戳>
< /安全>
< / S:标题>
< S:Body>
< getDocumentsResponse xmlns =http://asg.com/2009/03/schemas/tciadapters/getdocument>
<结果>
< operationStatus>
<严重程度> INFO< /严重程度>
< reasonCode> A300< / reasonCode>
< messageText>所有请求都没有错误完成。< / messageText>
< / operationStatus>
<文件>
< DocumentData>
< docId>
< NameValuePair>
< keyName> cmis:objectId< / keyName>
< keyValue> idd_48716F01-F5F7-4702-AC80-4EC70C949121< / keyValue>
< / NameValuePair>
< / docId>
< status xsi:nil =truexmlns:xsi =http://www.w3.org/2001/XMLSchema-instance/>
< content>
< ContentSegment>
<数据> aHR0cHM6Ly91c3J5ZTh0Y2k0bTAyLmFzZy5jb206NzQ0My9jbWlzYWRhcHRlci9nZXRjb250ZW50P3VzZXJpZD1kZXY2ZGV2aWQmcGFzc3dvcmQ9ZGV2ZWxvcCUyMzEmY21pc19pbnN0YW5jZT1GaWxlTmV0UDgrLStBdG9tUHViJnJlcG9zaXRvcnk9ZmlsZW5ldG9iamVjdHN0b3JlMSZjbWlzOm9iamVjdElkPWlkZF80ODcxNkYwMS1GNUY3LTQ3MDItQUM4MC00RUM3MEM5NDkxMjE = LT; /数据>
< / ContentSegment>
< / content>
< metadata />
<属性>
< subrange xsi:nil =truexmlns:xsi =http://www.w3.org/2001/XMLSchema-instance/>
< format xsi:nil =truexmlns:xsi =http://www.w3.org/2001/XMLSchema-instance/>
< supportedFormats />
< / properties>
<保留/>
< / DocumentData>
< / documents>
< / result>
< / getDocumentsResponse>
< / S:Body>
< / S:信封>


不幸的是,我运行最后一行代码时出现异常

解决方案

如果要在标签messageText中提取文本,则可以指定父节点和子节点,即修改您的最后一行

  def byteResponse = holder.getNodeValue(// *:operationStatus // *:messageText)

//由于在整个响应中只有一个名为messageText的标记,您可以使用这个

def byteResponse = holder.getNodeValue(// *:messageText )


My task is get value from xml in soapui to create next test in a sequence.

I use groovy script

My target is get text inside tag. It's easy to recognize becouse it's base64 encoded string.

def project = testRunner.testCase.testSuite.project ;
def tcase = project.testSuites["ChangeRetentionByCopyDemoSuite"].testCases["Milestone2"] ; 
def tstep = tcase.getTestStepByName("getDocuments - GetContentURLRequest");

def responseTestSuite1 = tstep.getPropertyValue("response");

log.info(responseTestSuite1.toString());

def gutils = new com.eviware.soapui.support.GroovyUtils( context );
def holder = gutils.getXmlHolder("$responseTestSuite1");

def byteResponse = holder.getNodeValue("/S:Envelope/S:Body/g:getDocumentsResponse/text()");

log.info(byteResponse);

The xml responce in getDocuments - GetContentURLRequest

    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Header>
      <Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
         <Timestamp xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <Created>2015-09-15T13:57:50.844Z</Created>
            <Expires>2015-09-16T13:57:50.844Z</Expires>
         </Timestamp>
      </Security>
   </S:Header>
   <S:Body>
      <getDocumentsResponse xmlns="http://asg.com/2009/03/schemas/tciadapters/getdocument">
         <result>
            <operationStatus>
               <severity>INFO</severity>
               <reasonCode>A300</reasonCode>
               <messageText>All requests completed without errors.</messageText>
            </operationStatus>
            <documents>
               <DocumentData>
                  <docId>
                     <NameValuePair>
                        <keyName>cmis:objectId</keyName>
                        <keyValue>idd_48716F01-F5F7-4702-AC80-4EC70C949121</keyValue>
                     </NameValuePair>
                  </docId>
                  <status xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  <content>
                     <ContentSegment>
                        <data>aHR0cHM6Ly91c3J5ZTh0Y2k0bTAyLmFzZy5jb206NzQ0My9jbWlzYWRhcHRlci9nZXRjb250ZW50P3VzZXJpZD1kZXY2ZGV2aWQmcGFzc3dvcmQ9ZGV2ZWxvcCUyMzEmY21pc19pbnN0YW5jZT1GaWxlTmV0UDgrLStBdG9tUHViJnJlcG9zaXRvcnk9ZmlsZW5ldG9iamVjdHN0b3JlMSZjbWlzOm9iamVjdElkPWlkZF80ODcxNkYwMS1GNUY3LTQ3MDItQUM4MC00RUM3MEM5NDkxMjE=</data>
                     </ContentSegment>
                  </content>
                  <metadata/>
                  <properties>
                     <subrange xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <format xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <supportedFormats/>
                  </properties>
                  <retention/>
               </DocumentData>
            </documents>
         </result>
      </getDocumentsResponse>
   </S:Body>
</S:Envelope>

Unfortunately I got exception when run last line of code

解决方案

If you want to extract text inside the tag "messageText" then, you could specify the parent node and the child node, i.e., modify your last line to

def byteResponse = holder.getNodeValue("//*:operationStatus//*:messageText")

// since there is only one tag named "messageText" in the entire response, you could use this also

def byteResponse = holder.getNodeValue("//*:messageText")

这篇关于在soapui groovy脚本中从xml获取文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆