How can I get a value for a variable and use it in other methods?

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

How does your main file actually run the scenario? The code seems to be written that thinks this items are executed in sequence.

Login.login(username, password)

won’t actually run the login it is just returning a builder, so it doesn’t make sense when its trying to get the string value directly out in the next line.
If you want to access the token that is saved I think you would just chain it to the login so something like.

Login.login(username, password) .exec(session => { val accessToken = session("loginToken").as[String] //do something with access token session })

Thank you for your response, and I apologize. I did chop some of the code out, trying to just include the pertinent information, and I guess I wasn’t thinking about that portion holding weight. Here is an updated version of my main file, with the scenario running code:

class Test extends Simulation {

val username = “johnDoe”
val password = “secretPhrase”

val newLogin = Login.login(username, password)
val token = Login.getToken().toString

val nextFunction = Function.function(username, token)

val t_iterations = Integer.getInteger(“iterations”, 1).toInt
val t_concurrency = Integer.getInteger(“concurrency”, 1).toInt
val t_rampUp = Integer.getInteger(“ramp-up”, 10).toInt

val httpConf = http
.baseURL(“https://funwebsite.com”)
.headers(Map(“Content-Type” → “application/json,text/html,application/xhtml+xml,application/xml”, “charset” → “UTF-8”))
.doNotTrackHeader(“1”)
.acceptLanguageHeader(“en-US,en;q=0.5”)
.acceptEncodingHeader(“gzip, deflate”)
.userAgentHeader(“Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0”)

val scn = scenario(“Test”)
.repeat(t_iterations){
exec(newLogin, nextFunction)
}

setUp(scn.inject(rampUsers(t_concurrency) over (t_rampUp)))
.protocols(httpConf)
}

Is there a way I can include this into the scenario execution? How can I get the token variable in my main test file to have the value of accessToken and use it in the nextFunction method? Thanks for the help.

Rob