2012-06-26 34 views
1

我尝试使用groovy脚本和soapUI自动化测试用例。在groovy context.expand表达式中使用变量

发送肥皂请求,我收到了一个包含公司列表的响应。 我想要做的是验证上市公司的名称。 响应数组的大小不固定。

所以,我想下面的脚本只是开始,但我卡住了..

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 

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

回答

3

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

这就是为什么错误是关于def第6行,并没有任何与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) 
} 

(如果你用it更换i瓶盖内,您也可以删除i ->。)