Global shared token is populated blank

I have a test in Scala…I declared a global variable to share the token with the other user, like advised. however when the next scenario is invoked, access token is null instead of the actual value form global token variable, can you please advise me what i am missing here?

var globalToken = "";

//chainbuilder to get the token
def auth_token: ChainBuilder =
     pause(pauseBy(5) seconds)
    .exec(
      http("Token")
        .post("https://token.com")
        .basicAuth("346394", "we4rywery")
        .queryParam("scope", "read write")
        .headers(header)
        .check(status.is(200)).check(jsonPath("$.access_token").exists.saveAs("access_token")))
      .exec(session => {
        globalToken = session("access_token").as[String].trim  ---> i assigned the token here, looks like it is not assigning as access_token is null for order scenario
        println(session) 
        session
      })

//Token is refreshing here as expected.
 val executionTime = 4.hours
  val authTimeout =5.minutes
  val safetyMargin = 30.seconds

 val authenticate: ChainBuilder = exec(auth_token)
      .exec(session => session.set("timeout", authTimeout.fromNow))

    private val refreshToken: ScenarioBuilder = scenario("Refresh token")
      .exec(authenticate)
      .during(executionTime) {
        doIf(session => {
          session("timeout").as[Deadline].timeLeft <= safetyMargin
        }) {
          exec(authenticate)
        }
      }


private val order: ScenarioBuilder =
    scenario("order")
      .exec(session => session.set("access_token", globalToken)) --> set the global token here, but it is null
     .exec(getOrder)


setUp(
    refreshToken.inject(atOnceUsers(1)),
    order.inject(nothingFor(3), atOnceUsers(1))

I really don’t think globalToken can possibly be null as it’s initiated with "".

sorry my bad, what i meant was it is blank, (not null), actual token is not assigned. Can you please advise if i am missing anything? i followed all the steps as advised.

Your code’s logic seems correct, meaning that:

  • either your auth_token chain fails
  • or the 3 seconds delay is not enough for it to complete

Lower the logging level to debug to figure that out.

ok. auth token succeeds and i see actual token in the json response, will try adding a more delay and check.

//chainbuilder to get the token
def auth_token: ChainBuilder =
     pause(pauseBy(5) seconds)
    .exec(
      http("Token")
        .post("https://token.com")
        .basicAuth("346394", "we4rywery")
        .queryParam("scope", "read write")
        .headers(header)
        .check(status.is(200)).check(jsonPath("$.access_token").exists.saveAs("access_token")))
      .exec(session => {
        globalToken = session("access_token").as[String].trim 
        println(session) 
        session.   ----------> can i return session here like this?
      })


private val order: ScenarioBuilder =
    scenario("order")
      .exec(session => session.set("access_token", globalToken)) 
     .exec(getOrder)

is session.set causing any issues? i read that session is immutable, wondering if i am doing anything wrong here when i set the session …

auth token succeeds and i see actual token in the json response

In your println too?

session. ----------> can i return session here like this?

Minus the dot, yes

is session.set causing any issues?

No. You use it the correct way.

Are you sure your issue is not actually in getOrder?

yes when session gets printed, i see the actual token getting printed on the terminal.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.