Defining a local variable at runtime

I lack a token service in my project, but I know a token to be used at runtime (inside the .exec) to get my test started.

I want to define a local variable as i.e. val token = “token_value” inside here below and reuse it further.

`
class AppSimulation extends Simulation {

val httpConf = http
.baseURL(“https://mysite.myserver.com”)
.acceptEncodingHeader(“gzip,deflate”)
.headers(Map(“Content-Type” → “application/json”, “charset” → “UTF-8”))

val scn = scenario(“Scenario”)
//HERE: val token = “token_value”
.feed(csv(“memberInfo.csv”).random)
.exec(
http(“getSite”)
.get("/user/profile")
.header(“X-Token”, “$token_value”)
.check(jsonPath("$.resultCode").is(“SUCCESS”))

`

Where should I define the local variable ‘token_value’ to make my example above to work?

Thank you,

Magnus

val scn = scenario("Scenario") .exec( session => session.set( "TOKEN", "value" ) ... .header( "X-Token", "$TOKEN" )

for multiple variables, you need to set it all at the same time.

`
session.set(“foo”, foo).set(bar"bar",bar)

`

Great, thanx!

But I get error messages as attached, when I do as you say John.

This is my simulation class (maybe you could paste it into your dev tool and see if you can find the fault?):

mport io.gatling.core.Predef._
import io.gatling.http.Predef._

class AppSimulation extends Simulation {

val httpConf = http
.baseURL(“https://google.com”)
.acceptEncodingHeader(“gzip,deflate”)
.headers(Map(“Content-Type” → “application/json”, “charset” → “UTF-8”, “User-Agent” → “Android(4.4)/Home(0.4)/0.1”))

val scn = scenario(“Scenario”)
.feed(csv(“memberInfo.csv”).random)
.exec(session => session.set( “TOKEN”, “value” )
http(“getProfile”)
.get("/user/profile")
.header( “X-Token”, “$TOKEN” )
.check(status.is(200))
.check(jsonPath("$.resultCode").is(“SUCCESS”))
.check(jsonPath("$.profile.memberships[0].scanNumber").saveAs(“scanNr”)))

.exec(session => {
println((session(“scanNr”).as[String]))
session
})

.exec(
http(“getCouponList”)
.get("/coupon/all")
.header(“X-Token”, “$TOKEN”)
.check(status.is(200))
})

setUp(scn.inject(constantUsersPerSec(1) during (1))).protocols(httpConf)

You have a syntax error.

The exec( http()…) must be separate from the exec( session => session.set( … ) )

.exec( _.set( “TOKEN”, “value” ) )
.exec( http( name )… )

Great, Thank you, it is working now.

Magnus