2016-12-07 67 views
-1

必須在如下肥皂UI請求加3個記錄:我如何ID值從響應存儲在soapUI的

<soapenv:Envelope> 
<soapenv:Header/> 
<soapenv:Body> 
<wor:AddValues> 
<wor:User> 
     <data:userid>${Properties1#UserID}</data:userid> 
</wor:User> 
<data:ValuesList> 
<data:Value> 
     <data:Desc>${Properties1#Desc1}</data:Desc> 
     <data:Details>${Properties1#Detail1}</data:Details> 
</data:Value> 
<data:Value> 
     <data:Desc>${Properties1#Desc2}</data:Desc> 
     <data:Details>${Properties1#Detail2}</data:Details> 
</data:Value> 
<data:Value> 
     <data:Desc>${Properties1#Desc3}</data:Desc> 
     <data:Details>${Properties1#Detail3}</data:Details> 
</data:Value> 
</data:ValuesList> 
</wor:AddValues> 
</soapenv:Body> 
</soapenv:Envelope> 

完成後對添加的每個值,這將產生唯一的ID。 響應看起來象下面這樣:

<s:Envelop> 
<s:Body> 
<AddUserValueResult> 
<a:ValuesList> 
<a:Value> 
     <a:Id>2501</a:Id>   
     <a:Desc>Desc1</a:Desc> 
     <a:Details>Detail1</a:Details> 
</a:Value> 
<a:Value> 
     <a:Id>2502</a:Id>   
     <a:Desc>Desc2</a:Desc> 
     <a:Details>Detail2</a:Details> 
</a:Value> 
<a:Value> 
     <a:Id>2503</a:Id>   
     <a:Desc>Desc3</a:Desc> 
     <a:Details>Detail3</a:Details> 
</a:Value> 
</a:ValuesList> 
</AddUserValueResult> 
</s:Body> 
</s:Envelop> 

使用Groovy腳本我想使用性能DESC1 &點評詳情, 與響應進行比較,如果匹配,則獲得該記錄的ID和存儲生成的ID在另一個屬性Id1中的響應。 對於其他兩條記錄繼續相同。 你能幫我實現嗎? 在此先感謝!

+0

PS:我使用SOAPUI 5.2.1,肥皂UI Pro或準備API解決方案不會有所幫助 –

+0

您可以發佈完整的請求和響應?當然,不必是實際的,樣本數據也可以做到。 – Rao

+0

用樣品請求更新了問題。這是我可以分享的大部分實際請求。請建議 –

回答

0

這裏是爲了能夠將數據從請求和響應比較做的事情之一:

  • 創建一個對象模型,這樣既請求和響應可以轉化爲對象的名單,然後進行比較。
  • 從請求/響應中提取所需的數據並構建對象列表
  • 需要考慮請求和響應都有數據/值列表這一事實,並且它可能以任何隨機順序出現。所以,對象應該是可比的。

在這種情況下,Script Assertion可用於比較而無需任何額外的測試步驟。因此,將腳本斷言添加到Soap Request測試步驟。

下面是腳本:

/** 
* This is script assertion 
* Reads both request and response 
* Builds the object list, so that both can be compared 
**/ 

//Object Model, req/resp is transformed into this model 
@groovy.transform.Canonical 
@groovy.transform.Sortable 
class Model { 
    String desc 
    String details 
} 

//Closure to retrieve the data element of user choice from xml 
def searchData = { data, element -> 
    def parsedData = new XmlSlurper().parseText(data) 
    parsedData.'**'.findAll{it.name() == element} 
} 

//Closure to build the object list of above provided Model class 
def buildData = { list, desc, details -> 
    def objects = [] 
    list.each { item -> 
     objects << new Model(desc: item."$desc", details: item."$details") 
    } 
    objects 
} 

//Assert if the request and response are non-empty 
assert context.rawRequest, "Request is empty or null" 
assert context.response, "Response is empty or null" 

//Get the Value elements from both request and response 
def reqValues = searchData(context.rawRequest, 'Value') 
def respValues = searchData(context.response, 'Value') 

//Build the Model object list for both request and response and sort them to be able to compare 
def reqObjectList = buildData(reqValues, 'Desc', 'Details').sort() 
def resObjectList = buildData(respValues, 'Desc', 'Details').sort() 

//Log the above 
log.info "Value objects from request: ${reqObjectList.toString()}" 
log.info "Value objects from response: ${resObjectList.toString()}" 

//Finally, compare the data from both request and response 
assert reqObjectList == resObjectList, "Data is not matching" 

//Now set the Ids at test case level custom properties 
respValues.eachWithIndex { value, index -> 
    log.info "Id${index+1} : ${value.Id as String}" 
    context.testCase.setProperty("Id${index+1}", "${value.Id as String}") 
} 

現在你可以使用${#TestCase#Id1}等。在任何你需要的值。 請注意:如果只有兩個ValuesList匹配,它纔會保存這些ID。
你可能很快嘗試一下從Demo直到比較請求和響應valueslist

相關問題