首页 文章

SoapUI与Groovy Script读取值

提问于
浏览
1

使用Groovy的SoapUI我正在使用SoapUI pro和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>

现在我想计算Customers中Person对象的总数(即在这种情况下答案是2) . 我尝试使用while循环,但它对我不起作用 . 任何人都可以告诉我如何使用循环?

提前致谢

1 回答

  • 1

    要计算 <Customers> 内所有 <Person> 的出现次数,您可以使用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()
    

    希望这可以帮助,

相关问题