如何将XPath添加到XmlSlurper

问题描述 投票:0回答:1

我试图在SoapUI中创建一个groovy脚本断言。在响应中,我试图拉一个叫写的字段。但是这些领域中有+15个。

我可以将XPath添加到XmlSlurper中以查找我想要断言的确切写入字段。

看下面的XML响应,我想断言b:premium \ written中的值。不是b中的那个:其他。鉴于有15个以上的b:写入字段,我想使用xpath声明该值。

XML响应:

<s:Body>
    <NewRateResponse>
        <NewRateResult>
            <b:policies>
                 <b:other>
                     <b:written>00.00</b:written>
                 </b:other>
                 <b:premium>
                     <b:written>31.21</b:written>
                 </b:premium>
            </b:policies>
        </NewRateResult>
    </NewRateResponse>
</s:Body>

断言代码:

import org.junit.Assert
def response = messageExchange.getResponseContent()
def xml = new XmlSlurper().parseText(response)
def nodePremium = xml.'**'.find { it.name() == 'written'}
Assert.assertEquals(00.01, nodePremium.toDouble(),0)

我相信我们需要改变的领域是def nodePremium = xml.'**'.find { it.name() == 'written'}。像def nodePremium = xml.'**'.find { it.name() == 'premium\written'},但这对我不起作用。

groovy soapui assertions
1个回答
1
投票
assert xml.'**'.find { it.name() == 'premium'}.written.text() as Double == 31.20
© www.soinside.com 2019 - 2024. All rights reserved.