user6221615
user6221615

Reputation: 1

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)

Upvotes: 0

Views: 1879

Answers (1)

albciff
albciff

Reputation: 18517

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,

Upvotes: 1

Related Questions