Ok, I’ve altered my code a bit and hopefully, this question is more reasonable.
I have a test I am designing that requires the testing of a lot of different functions. I have created a main file that can easily be updated with changing variables and rearranged so the test can be altered however required. I have then created a separate file that links to the main file for each of the functions we are testing.
So far, I’ve not had any problems with this, but now I am trying to pass information back and forth through the files. One of the functions is a login function that once is performed, a token is produced. I want to pass this token back to the main file to be used as a parameter in other methods being called from other function files.
I have been able to save the token using the “.saveAs(“variablename”)” method, which then I can access in other areas of the file using “${variablename}”, but I have not been able to return it to the main file in the required string format for future use.
Here is an example of my code:
Main File:
class Test extends Simulation {
val username = “johnDoe”
val password = “secretPhrase”
val newLogin = Login.login(username, password)
val token = Login.getToken().toString
}
Function File:
object Login {
def login ( username:String, password:String) : ChainBuilder = {
val UN = username
val PW = password
val jsonrequest = “”"{“user”:"""" + UN + “”"",
“password”:"""" + PW + “”""
}"""
val access = exec(http(“Login”)
.post("/URI/for/function/")
.body(StringBody(jsonrequest))
.transformResponse{
case response if response.isReceived =>
new ResponseWrapper(response) {
val stripped = response.body.string
override val body = new StringResponseBody(stripped, response.charset)
}
}
.asJSON
.check(jsonPath("$…token")
.find
.saveAs(“loginToken”))
return access
}
def getToken() : String = {
val token = exec(session => {
val accessToken = “${loginToken}”
session
})
return token
}
}
Please let me know if you can help. Thanks in advance.
Rob