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
But when i call from another class from another testsuite it says it cannot find expected_value.
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
For more details, please refer below thread
https://community.smartbear.com/t5/SoapUI-Pro/Calling-custom-Groovy-script-from-Script-Assertion-in-...
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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...
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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. ⬇️⬇️⬇️
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@_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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
no it doesn't resolve the probleme, the context is not the same.
solution is in progress....
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
