2013-12-23 45 views
3

我正在使用groovy脚本来验证对我的SoapUI xml请求的响应。使用Groovy解析SoapUI Pro中的XML响应

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

XML元素= // NS1:warningCode [1] 预期值= W0026

我的问题是,有时我的XML响应除了会,我想验证

一个返回其他警告代码

eg我可能会得到下面作为我的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"; 

} 

任何帮助将不胜感激。

谢谢。担。

回答

2

更简单的方法是使用XmlSlurper或XmlParser。下面我根据警告节点声明的名称空间(在这个问题提供的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() 

以上的推断确保预期的警告代码应出现在从响应中获得的任何一个代码中。