带Groovy脚本读取值的SoapUI

带Groovy脚本读取值的SoapUI,groovy,soapui,Groovy,Soapui,带Groovy的SoapUI 我正在使用SOAPUIPro和groovy脚本。我正在将客户记录从请求读入以下内容 def CustRec = context.expand('${GetProductPriceOffer#Request#/tem:request[1]/quot:Customers[1]}' ) CustRec中的值为 <quot:Customers> <quot:Person> <quot:CustomerType>PRIMARY</q

带Groovy的SoapUI 我正在使用SOAPUIPro和groovy脚本。我正在将客户记录从请求读入以下内容

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>

主要的,重要的
0
成人
1.
现在我想计算客户中Person对象的总数(即,在这个场景中答案是2)。我尝试了while循环,但它对我不起作用。 有人能告诉我如何使用循环实现吗


提前感谢

要统计
内部出现的所有
,您可以使用xpath函数,如下所示:

def numPersons =
context.expand('${GetProductPriceOffer#Request#count(//*:Customers/*:Person)}')
另一种可能是使用xpath而不是xpath,使用它可以计算
的出现次数,但如果需要执行更多操作,可以轻松地操作Xml。要计算
,可以使用以下方法:

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()

希望这有帮助,

显示while循环的代码。groovy代码段指定只从客户处获取一条记录。这是您在循环之前使用的代码段,还是您修改了它?我没有修改任何内容。嗨,albciff,非常感谢您的帮助。这两种解决方案对我都有效。我现在刚开始编程和学习。你能给我一些建议吗?在哪里可以找到关于SoapUI Groovy脚本的文档?Srini@Sirini您可以在查看官方groovy语言页面文档。此外,您还可以查看一些SOAPUI文档,了解如何使用groovy实现特定的SOAPUI功能,这里有一个关于如何在不同测试级别上访问请求、响应和getProperties的示例。希望这有助于<代码>:< /代码> @ Sirni,如果我的答案解决了你的问题,请考虑<代码>:<代码>