Forum Discussion

_ivanovich_'s avatar
_ivanovich_
Frequent Contributor
6 years ago

How to call a variable from another groovy code?

Hi,

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

A.groovy

VAR_fileName = 'Test2 Report.txt'

 

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

B.groovy

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(ClassCodeVisitorSupport.java:149) at org.codehaus.groovy.control.ResolveVisitor.visitClass(ResolveVisitor.java:1225) at org.codehaus.groovy.control.ResolveVisitor.startResolving(ResolveVisitor.java:178) at org.codehaus.groovy.control.CompilationUnit$11.call(CompilationUnit.java:651) at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:931) at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:593) at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:542) at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298) at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268) at groovy.lang.GroovyShell.parseClass(GroovyShell.java:694) at groovy.lang.GroovyShell.parse(GroovyShell.java:706) at groovy.lang.GroovyShell.parse(GroovyShell.java:742) at groovy.lang.GroovyShell.parse(GroovyShell.java:733) at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.compile(SoapUIGroovyScriptEngine.java:136) at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:87) at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:141) at com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction$1.run(GroovyScriptStepDesktopPanel.java:250) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) 1 error

  • 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:

    ${#TestCase#VAR_fileName}

     

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

    ${VAR_fileName}
  • JHunt's avatar
    JHunt
    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:

    ${#TestCase#VAR_fileName}

     

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

    ${VAR_fileName}