【问题标题】:Parsing XML response in SoapUI Pro using Groovy使用 Groovy 在 SoapUI Pro 中解析 XML 响应
【发布时间】:2013-12-23 14:16:36
【问题描述】:

我正在使用一个 groovy 脚本来验证对我的 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 元素。

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

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";

}

任何帮助将不胜感激。

谢谢。谭。

【问题讨论】:

    标签: xml groovy soapui


    【解决方案1】:

    更简单的方法是使用 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()
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多