简体   繁体   English

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

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

I am writing a script assertion to see if a element value contains in the array list and if it does, it passes. 我正在编写脚本断言,以查看元素值是否包含在数组列表中,如果包含,它将通过。

When I print the element:Number, I get like an example [1,2,3,3] as array. 当我打印element:Number时,我得到的例子是[1,2,3,3]作为数组。 If Number contains say 3, script has to pass. 如果数字包含说3,则脚本必须通过。

I have written below code which is failing, probably because the written value is an array list, how to assert an array value? 我在下面编写了失败的代码,可能是因为写入的值是一个数组列表,如何声明一个数组值?

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

The problem is that invoiceNumber is a Collection of groovy.util.slurpersupport.NodeChild elements instead of Integer elements. 问题是invoiceNumbergroovy.util.slurpersupport.NodeChild元素的Collection ,而不是Integer元素。 This is why the contains(3) comparison never returns true . 这就是为什么contains(3)比较永远不会返回true

You've to convert array of groovy.util.slurpersupport.NodeChild to array of integers before the contains() , you can do it using the spread dot operator an NodeChild.toInteger() , so your script must be: 您必须将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)

Hope it helps, 希望能帮助到你,

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

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