簡體   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