Search code examples
gatlingscala-gatling

Gatling not storing value in session


I am using sessions in gatling to store values, as shown below

      exec(session => {
        val id = Instant.now.toEpochMilli.toString + scala.util.Random.nextInt(1000).toString
        session.set("STARTED_PROCESS_ID",id)
        //Store the id somewhere for processing later
        session
      })
      .exec(
          http("scenario")
            .post(url)
            .header("Content-Type", "application/json")
            .header("id", session => session("STARTED_PROCESS_ID").as[String])
            .body(StringBody(body)
            .check(status.is(200))

According to the documentation, the value should be stored in session & the header "id" should be populated as expected. But when running the simuation I get the following error

java.util.NoSuchElementException: No attribute named 'STARTED_PROCESS_ID' is defined
    at io.gatling.core.session.SessionAttribute.as(Session.scala:46)
    at common.HttpUtil$.$anonfun$sendPostRequestForWasStartDefLoad$1(HttpUtil.scala:557)
    at io.gatling.core.action.SessionHook.execute(SessionHook.scala:32)
    at io.gatling.core.action.Action.$bang(Action.scala:38)
    at io.gatling.core.action.Action.$bang$(Action.scala:38)

Can someone please help explain why is this happening ?


Solution

  • You're not using the Session API correctly. Please properly read the documentation.

    Session is immutable and set returns a new instance.

    exec { session =>
        val id = Instant.now.toEpochMilli.toString + scala.util.Random.nextInt(1000).toString
        session.set("STARTED_PROCESS_ID",id)
    }