Getting multivalued session values

HI,

I’m very new to Scala and Gatling. I was exploring session attributes of Gatling. I need help in Get of multivalued session attributes on Gatling 2 M3a
I set a session attribute

val myMap = Map((“x”, email), (“y”, “dummy1”), (“z”, “dummy2”));
session.set(“abc”,myMap);

in subsequent Exec, I’m unable to compile the with,

val mm1=session(“abc”)(“x”);
println(“eMail:-----------------------------” +mm1);

What am I missing?

Here is the code:

val scn = scenario(“Scenario Name”)
.exec(
http(“request_1”)
.get("""/?key=new""")
.check(status.is(200))
.check(bodyString.saveAs(“getReply”))
)

.exec(session=>{
println(“getRreply:” +session(“getReply”).as[String]);
val cc=session(“getProduct_reply”).as[String];
val someXML = XML.loadString(cc)
val email = (someXML \ “EmailAddress”).text
println(“from xml:”)
println(city);

val myMap = Map((“x”, email), (“y”, “dummy1”), (“z”, “dummy2”));

session.set(“abc”,myMap);

})
.exec(session=>{
//println(“email address from person svc:” +session(“emailAddress”).as[String]);
val mm=session(“abc”) ;

println(“eMail:-----------------------------”+mm.toString());

//This throws compile error
//val mm1=session(“abc”)(“x”);
//println(“eMail:-----------------------------” +mm1);
session;
})

Thank you
shree

Hi,

session(“abc”) returns an Attribute. You have to use as, asOption or validate and a type in order to get a value out of it.

Just like you use session(“emailAddress”).as[String]), you can use session(“abc”).as(Map[String, String])

Thank you Stéphane for your quick response. I do need to research Scala Options, thanks for that tip.

However when I use val mm=session(“abc”).as(Map[String, String]);
I get the following compile error:

06:32:17.317 [ERROR] i.g.a.ZincCompiler$ - /Users/schatterjee1/dev/gatlingm3/user-files/simulations/PersonSvc.scala:54: missing arguments for method apply in class GenMapFactory;
follow this method with `_’ if you want to treat it as a partially applied function
06:32:17.319 [ERROR] i.g.a.ZincCompiler$ - val mm=session(“abc”).as(Map[String, String]);
06:32:17.320 [ERROR] i.g.a.ZincCompiler$ - ^

This compiles fine:

val scn = scenario(“Scenario Name”)
.exec(
http(“request_1”)
.get("""/?key=new""")
.check(status.is(200))
.check(bodyString.saveAs(“getReply”)))

.exec(session => {
println(“getRreply:” + session(“getReply”).as[String]);
val cc = session(“getProduct_reply”).as[String];
val someXML = XML.loadString(cc)
val email = (someXML \ “EmailAddress”).text
println(“from xml:”)
// println(city);

val myMap = Map((“x”, email), (“y”, “dummy1”), (“z”, “dummy2”));

session.set(“abc”, myMap);

})
.exec(session => {
//println(“email address from person svc:” +session(“emailAddress”).as[String]);
val mm: Map[String, String] = session(“abc”).as[Map[String, String]]

println(“eMail:-----------------------------” + mm.toString());

//This throws compile error
val mm1=session(“abc”).asMap[String, String];
//println(“eMail:-----------------------------” +mm1);
session;
})

I don’t know what your last code look like, so I can’t help there.

My bad, I was using .as(Map[String, String]) and not .as[Map[String, String]] (square brackets)
Thank you for your time in helping resolve this one. Appreciate it.

You’re welcome!