在groovy context.expand表达式中使用变量

时间:2012-06-26 04:42:06

标签: groovy expand

我尝试使用groovy脚本和soapUI自动化测试用例。

发送一个soap请求,我得到的回复包含公司列表。 我想做的是验证上市公司的名称。 响应数组的大小不固定。

所以我在开始时尝试了下面的脚本但是我卡住了..

def count = context.expand( '${Properties#count}' )
count = count.toInteger()
def i = 0
while (i<count)
    (
def response = context.expand( '${getCompanyList#Response#//multiRef['+i+']/@id}' )
 log.info(response)
i=İ+1   
    )

我得到了

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script12.groovy: 6: unexpected token: def @ line 6, column 1. def response = context.expand( '${getCompanyList#Response#//multiRef['+i+']/@id}' ) ^ org.codehaus.groovy.syntax.SyntaxException: unexpected token: def @ line 6, column 1. at

我应该以某种方式将“i”放在“响应”定义中。

1 个答案:

答案 0 :(得分:3)

你在while语句中使用了错误的字符,它应该是大括号({}),而不是括号(())。

这就是错误与第6行的 def 有关,而与i变量无关。

您的示例中还有İ,这在Groovy中不是有效的变量名。

我想你想要这个:

def count = context.expand( '${Properties#count}' )
count = count.toInteger()
def i = 0
while (i<count) {
    def response = context.expand( '${getCompanyList#Response#//multiRef['+i+']/@id}' )
    log.info(response)
    i=i+1   
}

然而,这是Groovy,您可以使用以下方式更清洁:

def count = context.expand( '${Properties#count}' )
count.toInteger().times { i ->
    def response = context.expand( '${getCompanyList#Response#//multiRef['+i+']/@id}' )
    log.info(response)
}

(如果您使用i替换封闭内的it,您也可以删除i ->

相关问题