xml - 使用 Groovy 在 SoapUI Pro 中解析 XML 响应

标签 xml groovy soapui

我正在使用常规脚本来验证对我的 SoapUI xml 请求的响应。

我有一个数据表,其中包含我的测试输入以及我想要在 xml 响应和预期结果中验证的元素的 xpath。

xml 元素 =//ns1:warningCode[1] 期望值 = W0026

我的问题是,有时我的 xml 响应会返回除我想要验证的代码之外的其他警告代码

例如作为我的 xml 响应的一部分,我可能会得到以下内容。

. . .

<NS1:departmentReference>200001060</NS1:departmentReference>
               <NS1:customerReference>invalid dept ref</NS1:customerReference>
               <NS1:senderReference>sendRef</NS1:senderReference>
            </NS1:requestedShipment>
         </NS1:completedShipmentInfo>
         <NS1:integrationFooter>
            <warnings xmlns="http://www.rmg.com/integration/core/V1">
               <warning>
                  <warningCode>W0022</warningCode>
                  <warningDescription>The customerReference specified is longer than 12 characters and has been truncated</warningDescription>
               </warning>
               <warning>
                  <warningCode>W0026</warningCode>
                  <warningDescription>The departmentReference specified is invalid and will be ignored</warningDescription>
               </warning>
            </warnings>

. . .

对于我的特定测试,我可能只想检查是否显示了第二个警告。这意味着我需要 a) 确保我的测试数据不会产生任何其他警告消息,或者 b) 知道将返回多少警告消息以及它们将以什么顺序显示,以便我的 xpath 是正确的。

我想知道如何重写我的脚本,以便如果有任何包含我期望的代码的 warningCode 元素,无论它是第一个也是唯一的还是第三个 warningCode 元素,它都会通过。

这是我用于验证的完整常规脚本......

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

def dataSource  = testRunner.testCase.getTestStepByName( "DataSourceShipmnt_v04" );

def groovyUtils     = new com.eviware.soapui.support.GroovyUtils(context);
def response        = context.expand( '${createShipmnt_v04#Response}' );     
def holder      = groovyUtils.getXmlHolder(response);   

//testElementOne will be something like '//ns1:warningCode[1]' or '//ns1:status/code'

def testElementOne = context.expand( '${DataSourceShipmnt_v04#testElement1}' ); 
def testElementTwo = context.expand( '${DataSourceShipmnt_v04#testElement2}' );

//expectedResp1 will be a warning code e.g W0026

def expectedResp1 = context.expand('${DataSourceShipmnt_v04#expectedResp1}');  
def expectedResp2 = context.expand( '${DataSourceShipmnt_v04#expectedResp2}'  ); 

def actRtrn1; 
def actRtrn2;

def result; //just a string value to return pass or fail.

try {

     actRtrn1       = holder.getNodeValue(testElementOne);
     actRtrn2       = holder.getNodeValue(testElementTwo);


}catch(Exception ex){}

if (  actRtrn1 == expectdResp1 && (actRtrn2 == expectdResp2 || actRtrn2 == null) ) {

  result = "pass";

} else {    

result = "fail";

}

如有任何帮助,我们将不胜感激。

谢谢。谭。

最佳答案

更简单的方法是使用 XmlSlurper 或 XmlParser。下面我已经根据Warning节点声明了命名空间(问题中提供的xml无效且不完整),您可以根据需要使用:

def xml = '''
<warnings xmlns="http://www.rmg.com/integration/core/V1">
       <warning>
          <warningCode>W0022</warningCode>
          <warningDescription>The customerReference specified is 
             longer than 12 characters and has been
               truncated</warningDescription>
       </warning>
       <warning>
          <warningCode>W0026</warningCode>
          <warningDescription>The departmentReference specified is 
                         invalid and will be ignored</warningDescription>
       </warning>
</warnings>
'''

def expected = 'W0026'
def slurper = new XmlSlurper().parseText(xml)
            .declareNamespace('ns2': 'http://www.rmg.com/integration/core/V1')
assert expected in slurper.warning.warningCode.collect()*.toString()

上述断言确保预期的警告代码应该出现在从响应中获得的任何代码中。

关于xml - 使用 Groovy 在 SoapUI Pro 中解析 XML 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20745363/

相关文章:

java - 根据 Eclipse 中通过 JAXB 和 XSD 构建选项生成的 xsd 文件验证 xml

objective-c - 从 NSWebView 获取 XML

grails - save()在Grails中如何工作?

soapui - 在 POSTMAN 中指定查询和模板参数

xml - WP7 从 xml Web 服务获取和存储数据以供整个项目使用

java - 最新的 Open JDK 8 JAXB 库无法解码具有包含换行符的属性的对象

grails - 如何在 Grails 2.4.X 中获取自定义数据源引用?

grails - 动态覆盖Mock()spock返回的对象的属性

java - 使用 SOAP UI 而不是 REST-assured 来自动化 CI 的 REST 服务是否有优势

SoapUI 第一个示例 - Saber