Forum Discussion
aaronpliu
4 years agoFrequent Contributor
Hi Learningbear ,
Using groovy's GString to accept variable.
In ReadyAPI, you may get variable with context.expand('${#TestCase#SSN}') or testRunner.testCase.getPropertyByValue("SSN").
If you want to use variable in json or other data structure (like map, list), then you can use variable directly or using GString format (i.e. "$SSN" or "${SSN}").
JSON / LinkedHashMap / ArrayList object would be serialization to json data with groovy.json.JsonSlurper
import groovy.json.JsonOutput
def SSN = context.expand('${#TestCase#SSN}')
def data = ["_id": "ObjectId(5e66a38b478d946943329bba)","name": "Tom", "age": 26, "ID": SSN, "Gender": "Male"
]
log.info JsonOutput.prettyPrint(JsonOutput.toJson(data))
// output
{
_id : ObjectId("5e66a38b478d946943329bba"),
name : "Tom",
age : 26, '
ID : "SD34re",
Gender : "Male"
}
Thanks,
/Aaron
Related Content
- 4 years ago
Recent Discussions
- 12 days ago
- 15 days ago