简体   繁体   中英

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. If Number contains say 3, script has to pass.

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. This is why the contains(3) comparison never returns 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:

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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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