2015-05-04 172 views
1

使用Groovy的SoapUI 我正在使用SoapUI pro和groovy腳本。我讀的客戶記錄從請求轉換成以下,使用Groovy腳本讀取值的SoapUI

def CustRec = context.expand('${GetProductPriceOffer#Request#/tem:request[1]/quot:Customers[1]}') 

在CustRec值,

<quot:Customers> 
<quot:Person> 
<quot:CustomerType>PRIMARY</quot:CustomerType> 
<quot:Sequence>0</quot:Sequence> 
</quot:Person> 
<quot:Person> 
<quot:CustomerType>ADULT</quot:CustomerType> 
<quot:Sequence>1</quot:Sequence> 
</quot:Person> 
</quot:Customers> 

現在我希望計算客戶Person對象的總數(即答案是2在這種情況下)。我嘗試while循環,但它並沒有爲我工作。 任何人都可以告訴我如何實現使用循環?

在此先感謝

+0

顯示了while循環的代碼。 – SiKing

+0

您的groovy片段指定僅從客戶那裏獲取一條記錄。這是您在循環之前使用的代碼段,還是您修改了它? –

+0

我沒有修改任何東西。 – Srini

回答

1

要計算的<Person>所有出現內部<Customers>可以使用count XPath函數如下:

def numPersons = 
context.expand('${GetProductPriceOffer#Request#count(//*:Customers/*:Person)}') 

另一種可能性是使用XmlSlurper,而不是使用XPath,有了它,你可以計算<Person>的出現次數,但是如果您需要執行更多操作,則可以通過簡單的方式操作Xml。計數<Person>你可以使用如下方法:

def custRec = context.expand('${GetProductPriceOffer#Request}') 
// parse the request 
def xml = new XmlSlurper().parseText(custRec) 
// find all elements in xml which tag name it's Person 
// and return the list 
def persons = xml.depthFirst().findAll { it.name() == 'Person' } 
// here you've the number of persons 
log.info persons.size() 

希望這有助於

+0

嗨albciff, 非常感謝你的幫助。這兩個解決方案爲我工作。 我現在是新手programmimg和學習。你能提出一些建議嗎? 我在哪裏可以找到有關SoapUI Groovy腳本的文檔? Srini – Srini

+0

@Sirini您可以在http://www.groovy-lang.org/documentation.html查看官方的常規語言頁面文檔。另外,你可以查看一些關於如何使用groovy實現特定的SOAPUI的SOAPUI文檔,[這裏](http://www.soapui.org/scripting---properties/tips---tricks.html)有一個例子如何在不同的測試級別訪問請求,響應,getProperties。希望這有助於':)' – albciff

+0

@Sirni也如果我的答案解決您的問題,請考慮[接受它](http://stackoverflow.com/help/accepted-answer)':)' – albciff

相關問題