1

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

发送一个肥皂请求,我得到一个包含公司列表的响应。我想做的是核实上市公司的名字。响应数组的大小不固定。

所以我一开始就尝试了下面的脚本,但我被卡住了..

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”放在“响应”定义中。

4

1 回答 1

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 ->。)

于 2012-06-26T05:56:26.700 回答