Forum Discussion

PramodYadav's avatar
PramodYadav
Contributor
6 years ago

Get Set and Reset Test case properties from Groovy script

Problem statement: If you are looking for a way to run individual groovy scripts (not full test case) while getting the context variable values set in another script, below may help you.

 

Note that the solution was already available on forums but parts of solution were scattered in multiple posts. Here I simply consolidate them in one place. Credits are at the end of this post.

 

Groovy scripts:

 

//Step1: For this example, Create two properties named zOSUserID and zOSPwd on the test case under which you are going to create below groovy script.

 

//Step2: Set TestCase parameters value (you can have this in one groovy script on two, depending on what your use case is)

 

context.testCase.properties["zOSUserID"].value = "fromGroovy"

context.testCase.properties["zOSPwd"].value = "dummy"

 

//Step3: Get TestCase properties value (again, you may have set the values in one script and may be using it in same or different scripts (mostly this case)

 

def userID = testRunner.testCase.properties["zOSUserID"].value //To get Testsuite property, replace testCase with testCase.testSuite

log.info "userID : $userID"

 

def password = testRunner.testCase.properties["zOSPwd"].value

log.info "password : $password"

 

//Step4: Reset each test case property value (This script, although can be in same script, but will usually be the last groovy script in test case, after which you want to start a new iteration

 

context.testCase.properties.each { context.testCase.properties[it.key].value = '' }

//( above was used for test case. For test suite replace context.testCase with context.testCase.testSuite in the code snippet.)

 

//To avoid getting script message

log.info "end"

 

Note: You can now run this groovy script and see how the properties are changed at test case level. Comment uncomment the reset part of above code to see how it affects test case parameters. Note that this work for simple variables but not for arrays. If it could work for arrays, that would have been really powerful (for debugging individual scripts at least). For now, even this, can help you where you don't need to pass arrays between scripts. Hope it helps.

 

Credits and references:

@nmaro : https://community.smartbear.com/t5/SoapUI-Pro/How-to-i-clear-the-property-values-of-a-TestCase-or-TestSuite/m-p/113726#M26452

krogold: https://community.smartbear.com/t5/SoapUI-Open-Source/How-to-write-a-property-value-in-testSuite-s-setup-script/m-p/152563#

omatzura: https://community.smartbear.com/t5/SoapUI-Open-Source/global-variables-between-suite-and-test-case/td-p/2099

No RepliesBe the first to reply