Forum Discussion

_ivanovich_'s avatar
_ivanovich_
Frequent Contributor
5 years ago

How to access a variable outside a method in groovy ?

Hi,

 i have a variable at project leve like: ${#Project#value} 

i have a groovy step with this:

//VAR
//def expected_value = testRunner.testCase.testSuite.project.getPropertyValue("value") as Integer

//TEST
def test_validate_name(){
def responseContent = testRunner.testCase.testSuite.project.getPropertyValue( "response" )
def slurperresponse = new JsonSlurper().parseText(responseContent)

float expected_value = 200
float[] a = slurperresponse.values.DATA.FILE.[0].value
float actual_value  = a.collect { "$it" }.join( '' ) as float
assert actual_value <= expected_value
}

It works if i keep this line inside the medthod:

float expected_value = 200

If i put comment on this line and use the variable from outside the method with this line:

def expected_value = testRunner.testCase.testSuite.project.getPropertyValue("value") as Integer

it throws error: java.lang.NullPointerException: Cannot invoke method test_validate_name() on null object

I tried with this set.context('{Project#value}') it doesn't work.

Do you have any suggestions ? 

 

  • Here is an example how to define the class defined in the script library, and usually making mistakes.

     

    /**
    * Class in the script library
    * which can be called or re-used from any groovy test step
    *
    **/
    class Utility {
        def showValue() {
    	log.info testRunner.testCase.testSuite.project.getPropertyValue( "USER_PROPERTY" )
        }
    }

    When user tried to use above showValue in groovy script:

     

    def util = new Utility()
    util.showValue()

    Then user would get below error:

       groovy.lang.MissingPropertyException: No such property: log for class: Utility

    The same is applicable for testRunner as well.

     

    That is because, only groovy script test step knows variables log, testRunner ; however, Class or its methods don't know them.

    Now the question is how access them in script library class methods?

    The answer is simple, pass those variables from groovy script test step as arguments to the method i.e., change the signature of showValue method to have log and testRunner as parameters.

     

    The changes are

     

    /**
    * Class in the script library
    * which can be called or re-used from any groovy test step
    *
    **/
    class Utility {
        def showValue(log, testRunner) {
    		log.info testRunner.testCase.testSuite.project.getPropertyValue( "USER_PROPERTY" )
        }
    }

    And statements in groovy script would be:

     

    def util = new Utility()
    util.showValue(log, testRunner)

    The above changes just works without error.

     _ivanovich_ see if the above example is helpful to resolve the issue.

     

  • groovyguy's avatar
    groovyguy
    Community Hero

    If I am not mistaken I am not sure that you can reference the variable OUTSIDE the method INSIDE the method without passing it in as a parameter. 

  • nmrao's avatar
    nmrao
    Champion Level 3

    _ivanovich_ 

    Couple of things:

     

    1. If that is the whole script, I could not see the need for the method.

    2. If method is needed, assuming that the method is called more than once in this script step

    3. the value is not accesssible because of the scope

    - a. either pass the value to the method

    say, def method(expectedValue) {...} and use method(variable)

    - b. compute the value from inside the method like you created responseContent

    • _ivanovich_'s avatar
      _ivanovich_
      Frequent Contributor

      nmrao 

      1. If that is the whole script, I could not see the need for the method:

      No it's not the whole script. This script is called from another class.

       

       

      • nmrao's avatar
        nmrao
        Champion Level 3
        Hope your question got answered.
  • nmrao's avatar
    nmrao
    Champion Level 3

    Here is an example how to define the class defined in the script library, and usually making mistakes.

     

    /**
    * Class in the script library
    * which can be called or re-used from any groovy test step
    *
    **/
    class Utility {
        def showValue() {
    	log.info testRunner.testCase.testSuite.project.getPropertyValue( "USER_PROPERTY" )
        }
    }

    When user tried to use above showValue in groovy script:

     

    def util = new Utility()
    util.showValue()

    Then user would get below error:

       groovy.lang.MissingPropertyException: No such property: log for class: Utility

    The same is applicable for testRunner as well.

     

    That is because, only groovy script test step knows variables log, testRunner ; however, Class or its methods don't know them.

    Now the question is how access them in script library class methods?

    The answer is simple, pass those variables from groovy script test step as arguments to the method i.e., change the signature of showValue method to have log and testRunner as parameters.

     

    The changes are

     

    /**
    * Class in the script library
    * which can be called or re-used from any groovy test step
    *
    **/
    class Utility {
        def showValue(log, testRunner) {
    		log.info testRunner.testCase.testSuite.project.getPropertyValue( "USER_PROPERTY" )
        }
    }

    And statements in groovy script would be:

     

    def util = new Utility()
    util.showValue(log, testRunner)

    The above changes just works without error.

     _ivanovich_ see if the above example is helpful to resolve the issue.

     

    • nmrao's avatar
      nmrao
      Champion Level 3

      Further improvement can be to make the method static to simplify the usage while calling the library methods if it does not require any instance specific variables and most of the libary cases here can be static unless some specifics need to create instance of the class.

      From

      def showValue(log, testRunner)



      To

      static def showValue(log, testRunner)


      And usage in the Groovy script test would not require to create instance of the class. Instead method can be directly called.

      Utility.showValue(log, testRunner)