繁体   English   中英

如何使用脚本断言在Groovy中断言数组值

[英]How to assert array values in groovy using script assertion

我正在编写脚本断言,以查看元素值是否包含在数组列表中,如果包含,它将通过。

当我打印element:Number时,我得到的例子是[1,2,3,3]作为数组。 如果数字包含说3,则脚本必须通过。

我在下面编写了失败的代码,可能是因为写入的值是一个数组列表,如何声明一个数组值?

def response = messageExchange.getResponseContent()
def xml = new XmlSlurper().parseText(response)
def invoiceNumber= xml.'**'.findAll { it.name() == 'Number'}
log.info "$invoiceNumber"
assert invoiceNumber.contains(1)

问题是invoiceNumbergroovy.util.slurpersupport.NodeChild元素的Collection ,而不是Integer元素。 这就是为什么contains(3)比较永远不会返回true

您必须将groovy.util.slurpersupport.NodeChild数组转换为contains()之前的整数数组,您可以使用扩展点运算符NodeChild.toInteger() ,因此脚本必须为:

def response = messageExchange.getResponseContent()
def xml = new XmlSlurper().parseText(response)
def invoiceNumber= xml.'**'.findAll { it.name() == 'Number'}
log.info "$invoiceNumber"
// convert the array to array of integers
invoiceNumber = invoiceNumber*.toInteger()
// now you can perform the assert correctly
assert invoiceNumber .contains(3)

希望能帮助到你,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM