Replace one part in a request with the value from an variable
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Replace one part in a request with the value from an variable
Hi,
Is there a way to replace the "XXXX" in the method below with the value of the variable replaceToken?
def replaceToken = "username"
parsedJson.secrets.XXXX.toString()
I.e., I would like to achieve following automatically
parsedJson.secrets.username.toString()
Seems like an easy thing when working with strings but when I want to have it in a call then it always interprets the replaceToken as "replaceToken" and not "username"
Solved! Go to Solution.
- Labels:
-
Scripting
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Have a look at http://blog.gtiwari333.com/2012/08/groovy-grails-dynamic-method-n-variable.html
You can call methods using variables and array elements.
I've created a small Groovy script as a demo. See below.
def myObj = new MyClass();
// Call in the usual way.
log.info("Call in the usual way.");
log.info(myObj.methodOne());
log.info(myObj.methodTwo());
log.info(myObj.methodThree());
log.info("-------------------------------------------");
// Call using a variable.
log.info("Call using a variable");
def methodToCall = "methodOne";
log.info(myObj."$methodToCall"());
methodToCall = "methodTwo";
log.info(myObj."$methodToCall"());
methodToCall = "methodThree";
log.info(myObj."$methodToCall"());
log.info("-------------------------------------------");
log.info("Call with method name in an array");
def myArray = ["methodOne", "methodTwo", "methodThree"];
log.info(myObj.(myArray[0])());
log.info(myObj.(myArray[1])());
log.info(myObj.(myArray[2])());
log.info("-------------------------------------------");
class MyClass {
MyClass(){
// empty constructor
}
def methodOne(){
return "I am method one";
}
def methodTwo(){
return "I am method two";
}
def methodThree(){
return "I am method three";
}
}
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Chris,
You are a true hero.. Thank you so much. It worked perfectly. Now I am able to read a .json file and set my project properties to the values from the secrets file. Without this I would have to declare every property manually which is not efficient at all. Great!!
//get secret file for the active environment
secretsJsonFile = projectpath + "ReadyApi_Secrets\\secrets_" + activeEnv + ".json" //get secrets file for the active environment
log.info("secretsJsonFile = " + secretsJsonFile)
// Read jsonfile and return text content
def secretsFile = new File(secretsJsonFile);
def parsedJson = new JsonSlurper().parseText(secretsFile.text)
def oneItem, oneItemValue
for(i = 0; i < numberOfProjectProperties; i++)
{
oneItem = projectPropertiesList[i] //get one item in the projectPropertiesList
oneItemValue = parsedJson.secrets."$oneItem".toString() //get the value of the item from the secrets file
log.info("oneItem = " + oneItem)
log.info("oneItemValue = " + oneItemValue)
//set global property value with value from secrets file
testRunner.testCase.testSuite.project.setPropertyValue( projectPropertiesList[i], oneItemValue)
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
You're welcome - I'm glad it helped.
Chris
