cancel
Showing results for 
Search instead for 
Did you mean: 

How to access a variable outside a method in groovy ?

SOLVED
Highlighted
Frequent Contributor

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 ? 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

Re: How to access a variable outside a method in groovy ?

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.

 



Regards,
Rao.

View solution in original post

21 REPLIES 21
Highlighted
Community Hero

Re: How to acces a varibale outside a method in groovy ?

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. 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
Highlighted
Community Hero

Re: How to acces a varibale outside a method in groovy ?

@_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



Regards,
Rao.
Highlighted
Frequent Contributor

Re: How to acces a varibale outside a method in groovy ?

@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.

 

 

Highlighted
Community Hero

Re: How to acces a varibale outside a method in groovy ?

Hope your question got answered.


Regards,
Rao.
Highlighted
Frequent Contributor

Re: How to acces a varibale outside a method in groovy ?

@nmrao 

if i get answer i will post here.

i did not figure out yet.

i was thinking about using context.expand for the variable.

 

Highlighted
Community Hero

Re: How to acces a varibale outside a method in groovy ?

Please see the comment

 

def test_validate_name(){
	def responseContent = testRunner.testCase.testSuite.project.getPropertyValue( "response" )
	def slurperresponse = new JsonSlurper().parseText(responseContent)
	//You can use as below
	float expected_value = testRunner.testCase.testSuite.project.getPropertyValue("value")
	float[] a = slurperresponse.values.DATA.FILE.[0].value
	float actual_value  = a.collect { "$it" }.join( '' ) as float
	assert actual_value <= expected_value
}


Regards,
Rao.
Highlighted
Frequent Contributor

Re: How to acces a varibale outside a method in groovy ?

@nmrao 

Hi,

i understand this and im using it actually.

suppose we have 2 methods:

def test_validate_name()

def test_validate_address()

I need to validate with the same variable expected_value

How do you delcare the variable?

The variable expected_value should be accessible everywhere, especially in these 2 methods.

This avoid the repetition of the following 3 lines in each methods.

float expected_value = testRunner.testCase.testSuite.project.getPropertyValue("value")
float[] a = slurperresponse.values.DATA.FILE.[0].value
float actual_value = a.collect { "$it" }.join( '' ) as float

The first method:

def test_validate_name(){
	def responseContent = testRunner.testCase.testSuite.project.getPropertyValue( "response" )
	def slurperresponse = new JsonSlurper().parseText(responseContent)
	//You can use as below
	float expected_value = testRunner.testCase.testSuite.project.getPropertyValue("value")
	float[] a = slurperresponse.values.DATA.FILE.[0].value
	float actual_value  = a.collect { "$it" }.join( '' ) as float
	assert actual_value <= expected_value
}


I hope i'm clear.

Community Hero

Re: How to acces a varibale outside a method in groovy ?

@_ivanovich_ 

Not sure If missed something.

 

Isn't this you wanted?

float expected_value = testRunner.testCase.testSuite.project.getPropertyValue("value")

def test_validate_name(expected){
	def responseContent = testRunner.testCase.testSuite.project.getPropertyValue( "response" )
	def slurperresponse = new JsonSlurper().parseText(responseContent)
	float[] a = slurperresponse.values.DATA.FILE.[0].value
	float actual_value  = a.collect { "$it" }.join( '' ) as float
	//Using below the passed value	
	assert actual_value <= expected
}

test_validate_name(expected_value)


Regards,
Rao.
Highlighted
Frequent Contributor

Re: How to acces a varibale outside a method in groovy ?

We are almost there but it's not working for me.

i modified these lines 

def test_validate_name(expected) to def test_validate_name(expected_value)
assert actual_value <= expected to assert actual_value <= expected_value

But when i call from another class from another testsuite it says it cannot find expected_value

It works if i put this line 

float expected_value = testRunner.testCase.testSuite.project.getPropertyValue("value")

 inside the method 

def test_validate_name()

 Can we just use def expected_value = context.expand('Project#value')

i don't know how to call this variable inside the method like:

assert actual_value <= expected_value 

 

Also i saw this in google:

import groovy.transform.Fieldvar1 = 'var1'
@Field String var2 = 'var2'
def var3 = 'var3'
void printVars() {    println var1
    println var2
    println var3 // This won't work, because not in script scope.
}