How to access a variable outside a method in groovy ?
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 ?
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
if i get answer i will post here.
i did not figure out yet.
i was thinking about using context.expand for the variable.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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. }
