Ask a Question

How to access a variable outside a method in groovy ?

SOLVED
_ivanovich_
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 ? 

 

21 REPLIES 21

We dont have visibility of the context. So, can't understand below statement.

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


Regards,
Rao.
_ivanovich_
Frequent Contributor

@nmrao 

please check image.

i tried to explain.

Had a feeling the usage is not correct. If you want to re-use some code, then you have to use script library.
For more details, please refer below thread
https://community.smartbear.com/t5/SoapUI-Pro/Calling-custom-Groovy-script-from-Script-Assertion-in-...


Regards,
Rao.
_ivanovich_
Frequent Contributor

@nmrao 

Hi,

I'm using library.

the whole project is running fine with library.

I cannot post the whole part of the code for library.

but i'm invesgating...

Nastya_Khovrina
SmartBear Alumni (Retired)

Hi @_ivanovich_,

 

I've checked your screenshot and have some comments:

- you cannot write a script at one Groovy script test step, and, then, use its methods in another part of the project;

- you said that you use libraries, so that shouldn't be the case, but, inside the method, you use a test step response. To make your test_validate_name method independent, you can pass both values as parameters. 

 

Note that there is the RunTestCase test step. You can write a groovy script in this test case and run it when it's needed with different parameters. Maybe, this will help you.


Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
sonya_m
SmartBear Alumni (Retired)

Thank you everyone for help!

 

Hi @_ivanovich_ , please share your progress with us. Have you solved the issue?

Perhaps, Nastya's latest comment could help straighten things out a bit?


Sonya Mihaljova
Community and Education Specialist

I think this screenshot makes me clear of your problem....please refer attached dummy project hope it will solve your problem..it explains 2 parts:

 

1) calling groovy script from same Test case

2) calling groovy script from diffrent Test Suite

 

 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal

@_ivanovich_  have you got any time to check the xml file which i have shared, hope it will resolve your issue, eager to know the result.... 🙂

 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal

@HimanshuTayal 

Hi,

no it doesn't resolve the probleme, the context is not the same.

solution is in progress....

@_ivanovich_ :

 

Didn't get "context is not the same", can you please breif more.


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
cancel
Showing results for 
Search instead for 
Did you mean: