Saving an attribute value using Session

Hello,

I am trying to save an attribute value to a variable, so that it can be used in further chains. Following is the snippet I am trying:

.exec(session => {
val VariableSelect = session(“Variable.random()”).as[String]
session.set(“VariableSelect”,VariableSelect)
})

Here “Variable” is the Vector containing multiple elements. If I use ${Variable.random()} in my request directly it returns one of the element, but since I am looking at doing some more pre-processing based on its value, need to get save it as a variable/attribute. I am getting following exception:

[ERROR] [12/02/2014 15:03:10.326] [GatlingSystem-akka.actor.default-dispatcher-11] [akka://GatlingSy
stem/user/$e] key not found: Variable.random()
java.util.NoSuchElementException: key not found: Variable.random()
at scala.collection.MapLike$class.default(MapLike.scala:228)
at scala.collection.AbstractMap.default(Map.scala:58)
at scala.collection.MapLike$class.apply(MapLike.scala:141)
at scala.collection.AbstractMap.apply(Map.scala:58)
at io.gatling.core.session.SessionAttribute.as(Session.scala:40)
at quickTest$$anonfun$1.apply(quickTest.scala:144)
at quickTest$$anonfun$1.apply(quickTest.scala:143)
at io.gatling.core.action.SessionHook.executeOrFail(SessionHook.scala:35)
at io.gatling.core.action.Failable$class.execute(Actions.scala:71)
at io.gatling.core.action.SessionHook.execute(SessionHook.scala:28)
at io.gatling.core.action.Action$$anonfun$receive$1.applyOrElse(Actions.scala:29)
at scala.PartialFunction$OrElse.applyOrElse(PartialFunction.scala:166)
at akka.actor.Actor$class.aroundReceive(Actor.scala:465)
at io.gatling.core.akka.BaseActor.aroundReceive(BaseActor.scala:23)
at akka.actor.ActorCell.receiveMessage(ActorCell.scala:516)
at akka.actor.ActorCell.invoke(ActorCell.scala:487)
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:238)
at akka.dispatch.Mailbox.run(Mailbox.scala:220)
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala
:393)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

15:03:10.346 [ERROR] i.g.c.a.b.SessionHookBuilder$$anonfun$build$1$$anon$1 - Action anonfunbuild1ano
n1 crashed on session Some(Session(

I am saving the Variable Map with following:

.check(jsonPath("$.entries[*].id").findAll.saveAs(“Variable”)))

Also I am able to use with Variable.random as below

.get(uri + “”"/${Variable.random()}""")

and following session.set is also working, so its problem when I am trying to extract an element from a vector and then save.

.exec(session => {
val VariableSelect = session(“test”).as[String]
session.set(“VariableSelect”,VariableSelect)
})

मंगलवार, 2 दिसम्बर 2014 3:13:33 pm UTC+5:30 को, Sameer Mainkar ने लिखा:

Please read the Expression documentation: it only works in some specific places. You wouldn’t expect JSP’s EL to work out of the box in standard Java code.

Ok, is there a way to do this in Gatling.

मंगलवार, 2 दिसम्बर 2014 5:22:45 pm UTC+5:30 को, Stéphane Landelle ने लिखा:

Have you tried saving it as a vector in session and getting the element from index.

something like

`

.exec(session => {
val VariableSelect = session(“test”).as[Vector[String]]
session.set(“VariableSelect”,VariableSelect(2)) //whatever index your element is saved at.

})

`

Unless I don’t the question, it could be simple as that.

Yep. Just use ThreadLocalRandom to compute a random index.

Thanks Abhinav. Was not aware of ThreadLocalRandom that can be used in this case, hence had ruled out that option, since wanted to go with random selection. Tried ThreadLocalRandom and its working, though had to use different format.

Any idea why Approach 1 didn’t worked:

Approach 1: Didn’t worked. Got error while compiling had included “import scala.concurrent.forkjoin.ThreadLocalRandom”

not found: value Variable
23:14:10.759 [ERROR] i.g.a.ZincCompiler$ - Variable ← session(“Variable”).validate[Seq[String]]
23:14:10.759 [ERROR] i.g.a.ZincCompiler$ - ^
23:14:10.774 [ERROR] i.g.a.ZincCompiler$ - one error found

.exec(session => {
for {

Variable ← session(“Variable”).validate[Seq[String]]
val VariableSelect = Variable(ThreadLocalRandom.current.nextInt(Variable.size))
}yield session.set(“VariableSelect”,VariableSelect)
})

Approach 2: -----------> This worked

.exec(session => {
session(“Variable”).validate[Seq[String]] match {
case Success(ids) =>
val index = ThreadLocalRandom.current.nextInt(ids.size)
session.set(“VariableSelect”, ids(index))
case _ => session
}
})

Thanks.

मंगलवार, 2 दिसम्बर 2014 9:47:33 pm UTC+5:30 को, Stéphane Landelle ने लिखा:

Just bring set outside braces
var VariableSelect = “”

.exec(session => {
VariableSelect = session(“Variable.random()”).as[String]
session
}.set(“VariableSelect”,VariableSelect))