FIDIL
FIDIL

Reputation: 117

using variables in groovy context.expand expression

I tried to automate a test case using groovy script and soapUI.

Sending a soap request, I got a response contains a company list. What I'ld like to do is verifying names of listed companies. Size of the response array is not fixed.

So I tried the script below just for the beginning but I got stuck..

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   
    )

I get

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 should somehow put "i" in the "response" definition..

Upvotes: 1

Views: 15704

Answers (1)

OverZealous
OverZealous

Reputation: 39580

You are using the wrong characters to in your while statement, it should be braces ({}), not parentheses (()).

This is why the error is regarding the def on line 6, and has nothing to do with the i variable.

You also have İ in your example, which is not a valid variable name in Groovy.

I think you wanted this:

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   
}

However, this being Groovy, you can do this much cleaner using:

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

(And if you replace i inside the closure with it, you can remove the i -> as well.)

Upvotes: 3

Related Questions