How do I send session values to a method

Hi!

I have a problem that I can’t get my head around.

I need to add a cookie based on some values I have stored in the session. My attempt, that do not work is explained below.
Neither addCookie or the generate function from my OAuthLoginSessionGenerator is run.

`

def addWebSSoCookies: ChainBuilder =
  exec(session =>
  {
    addCookie(Cookie("Session", new OAuthLoginSessionGenerator().generate(session("clientId").as[String], session("state").as[String])))
    session
  })

`

Any ideas on how to solve this?

Regards
Robert

It’s all on the documentation: https://gatling.io/docs/current/general/scenario/#exec

Warning

Gatling DSL components are immutable ActionBuilder(s) that have to be chained altogether and are only built once on startup. The results is a workflow chain of Action(s). These builders don’t do anything by themselves, they don’t trigger any side effect, they are just definitions. As a result, creating such DSL components at runtime in functions is completely meaningless. If you want conditional paths in your execution flow, use the proper DSL components (doIf, randomSwitch, etc)

exec { session =>

  if (someSessionBasedCondition(session)) {
    // just create a builder that is immediately discarded, hence doesn't do anything
    // you should be using a doIf here
    http("Get Homepage").get("[http://github.com/gatling/gatling](http://github.com/gatling/gatling)")
  }
  session
}
def addWebSSoCookies: ChainBuilder =
  exec(
    addCookie(Cookie("Session", session => new OAuthLoginSessionGenerator().generate(session("clientId").as[String], session("state").as[String])))
  )

Thank you!

Works great.