2013-12-23 47 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() 

以上的推斷確保預期的警告代碼應出現在從響應中獲得的任何一個代碼中。