Ask a Question

How to call a variable from another groovy code?

Frequent Contributor

How to call a variable from another groovy code?


i have a groovy step with only a variable like this:


VAR_fileName = 'Test2 Report.txt'


I want to call the variable VAR_fileName in another groovy code.


import A

def rootFolder =  RootResult + VAR_fileName


It returns error:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script11.groovy: 7: unable to resolve class A @ line 7, column 1. import A ^ org.codehaus.groovy.syntax.SyntaxException: unable to resolve class A @ line 7, column 1. at org.codehaus.groovy.ast.ClassCodeVisitorSupport.addError( at org.codehaus.groovy.control.ResolveVisitor.visitClass( at org.codehaus.groovy.control.ResolveVisitor.startResolving( at org.codehaus.groovy.control.CompilationUnit$ at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits( at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation( at org.codehaus.groovy.control.CompilationUnit.compile( at groovy.lang.GroovyClassLoader.doParseClass( at groovy.lang.GroovyClassLoader.parseClass( at groovy.lang.GroovyShell.parseClass( at groovy.lang.GroovyShell.parse( at groovy.lang.GroovyShell.parse( at groovy.lang.GroovyShell.parse( at at at at com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction$ at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$ Source) at Source) 1 error

Community Hero

There are two options. The first is to use Properties.

testRunner.testCase.setPropertyValue("VAR_fileName", "Test2 Report.txt")
def rootFolder = RootResult + testRunner.testCase.getPropertyValue("VAR_fileName")

When you set Properties, the advantage is they are saved permanently into the custom properties of the test case. So if you need to run only step B again, the value will still be there. And when you save your project, you will save the latest values for the properties.


The downside is that you can only set properties with String values. And you should be careful that you don't accidentally use a value from a previous run of the test case if it is no longer valid.


You can retrieve properties for use in your requests, too. Just add a property expansion, like this:



The other option is to use the context.

context.VAR_fileName = "Test2 Report.txt"
def rootFolder = RootResult + context.VAR_fileName

The context is shared between all the test steps. It can hold any type of data, and it is reset each time you run the test case.


When using the context, you will need to run the test case to pass values between steps. If you run only a single test step, that step is run with a brand new context.


You can use these values in requests too. The property expansion looks simply like this (no need to include the word context😞

Showing results for 
Search instead for 
Did you mean: