How to set session attribute with gatling 2?

Hi,

I’m using Gatling 2.0.0-M3a and I’m trying to set a session attribute but it gives errors however I try this.
The code bit is here:

.exec( session => {
import io.gatling.http.cookie._
import java.net.URI

val allCookies = session(“gatling.http.cookies”)
println("HERE ARE ALL THE COOKIES: " + allCookies)

val ck = session(“gatling.http.cookies”).as[CookieJar].get(URI.create("")).find(_.getName == “”)
println("HERE IS THE COOKIE: " + ck)
var cookieValue = ck.getOrElse(null).getValue
println("HERE IS THE COOKIE VALUE: " + cookieValue)

session.setAttribute(“myCookieValue”, ck.getOrElse(null).getValue)

session
})

.exec(http(“request_4”)
.post("/Login.jsp")
.headers(headers_4)
.param(""“DOM”"", “${myCookieValue}”)
)

This code throws a compilation error “value setAttribute is not a member of io.gatling.core.session.Session” even though in https://github.com/excilys/gatling/wiki/Session it says that it should be a member. I’ve also tried using saveAs, but it gives the error “not defined for Option” or “not defined for String” depending on where I use it.

How can i pass this cookie value as a parameter to the next request?
I’m a complete beginner in Scala and Gatling btw.

Thanks in advance,
TT

Ok, I found the Gatling 2 wiki page and tried to use the methods there:

.exec( session => {
import io.gatling.http.cookie._
import java.net.URI

val allCookies = session(“gatling.http.cookies”)
println("HERE ARE ALL THE COOKIES: " + allCookies)

val ck = session(“gatling.http.cookies”).as[CookieJar].get(URI.create("")).find(_.getName == “”)
println("HERE IS THE COOKIE: " + ck)
var cookieValue = ck.getOrElse(null).getValue
println("HERE IS THE COOKIE VALUE: " + cookieValue)

session.set(“myCookieValue”, ck.getOrElse(null).getValue)

session
})

.exec(http(“request_4”)
.post("/Login.jsp")
.headers(headers_4)
.param(""“DOM”"", session(“myCookieValue”).as[String])
)

Now the problem is, that session is undefined in the second block. How do I solve this?

Thanks in advance,
TT

Hi,

You’re missing an important point: https://github.com/excilys/gatling/wiki/Session#wiki-immutability
So, you’re not returning the new session containing “myCookieValue” (as returned by .set) but the original one.

Also, once you’re more familiar with Scala, try not to use null.

Here’s a version with Option:
.exec(session => {
import io.gatling.http.cookie._
import java.net.URI

val newSession = (session(“gatling.http.cookies”).asOption[CookieJar].flatMap { cookieJar =>
val cookie = cookieJar.get(URI.create("")).find(_.getName == “”)
cookie.map(session.set(“myCookieValue”, _))
}).getOrElse(session)
newSession
})

and one with Validation (better):

.exec(session => {
import io.gatling.http.cookie._
import java.net.URI

session(“gatling.http.cookies”).validate[CookieJar].map{cookieJar =>
val cookie = cookieJar.get(URI.create("")).find(_.getName == “”)
cookie.map(session.set(“myCookieValue”, _)).getOrElse(session)
}
})

Cheers,

Stéphane

Hi again,

Thanks for the prompt reply!

I understand that i didn’t return the new session but the old one, without the new session attribute added.

However I still don’t understand how to pass the cookie string value into the next exec http request? I only need to pass the value of a specific cookie as a parameter in the http request, nothing else.

.exec(http(“request_4”)
.post("/Login.jsp")
.headers(headers_4)

.param(""“DOM”"", session(“myCookieValue”).as[String])
)

This code fails because obviously “session” does not exist here. How do I do that?

Thanks for trying to help,
TT

This won’t compile.

Either pass an EL String, or a function:

.param(""“DOM”"", session => session(“myCookieValue”).as[String])

or

.param(""“DOM”"", “${myCookieValue}”)

Thanks a lot. That was the missing bit!

You made my day!
TT

You’re welcome, have fun!

Stéphane