Hot to use session value in another function gatling

10,268 views
Skip to first unread message

leela krishna

unread,
Aug 7, 2017, 4:13:09 PM8/7/17
to Gatling User Group
I'm trying to save the session value and use that in other function. Here is what I'm trying

.exec ( session => session.set("abc", session("value").as[String]))

.exec (test.authTest)

object test{
def authTest: String = {
val x = "${abc}"
return x
}
}

when I execute my gatling script, I'm seeing something like this 


16:05:17.388 [ERROR] i.g.c.a.b.SessionHookBuilder$$anon$1 - 'hook-1' failed to execute: Can't cast value  of type class java.lang.String into class io.gatling.core.session.Session

How can I save a session value and use that in my next method or request. Please help if anyone know about this.
Thanks in advance

Robert Bacon

unread,
Aug 7, 2017, 4:45:15 PM8/7/17
to Gatling User Group
Hi Leela,

I ran into this earlier on with my own test.  The key is to utilize a check to ensure you have the variable you are looking for, then to save it with saveAs.  For example:

.check("$..value")
.find
.saveAs("abc")


Then, when you want to use "abc" again later, you should be able to just call it using "${abc}".

I'll be honest, I had some problems with this, but my issue was I was unintentionally using both the """ triple quotes """ and the 's' for interpolation.  Hopefully this will help you out!!!

Rob
Message has been deleted

leela krishna

unread,
Aug 8, 2017, 10:19:48 AM8/8/17
to Gatling User Group
Hi Rob, 
I'm using a check in my HTTP request and then saved it as String like this 
val scn = scenario("Test")
.feed(csvFeeeder)
.exec(http("Header generation")
.post("/auth")
.headers(headers_0)
.formParam("xxxx", "zzzz")
.check(headerRegex("value","abc*").saveAs("value"))
.check(status.is(401)) )

after saving that, I'm trying to use session to store the header value and use that in a different function like this 
.exec ( session => session.set("abc", session("value").as[String]))

.exec (test.authTest)

object test{
def authTest: String = {
val x = "${abc}"
return x
}
}

But when I tried to get the session value, I'm seeing that error. I'm not sure what's wrong with the approach. 

Robert Bacon

unread,
Aug 8, 2017, 12:03:18 PM8/8/17
to Gatling User Group
I went through a lot of that too.  I went through the whole session.set and session("value").as[String] and none of them worked for me either.  What finally worked for me was to simplify it.

Try this:

.check(headerRegex("value","abc").saveAs("value"))

.exec(test.authTest)


object test{
def authTest:String = {
val x = "${value}"
return x
}
}


Though another issue I see that might cause you some problems is the defining of authTest after you call it.  Is there a way you can put the def authTest portion in front of the .exec(test.authTest) ?

leela krishna

unread,
Aug 8, 2017, 3:07:42 PM8/8/17
to Gatling User Group
I tried like this, but encountered with this error ';' expected but '.' found
object test{
def authTest: String = {
val x = "${value}"
println(x)
return x
}
}

.exec (test.authTest)

Robert Bacon

unread,
Aug 8, 2017, 3:52:31 PM8/8/17
to Gatling User Group
Try taking out the '.' before exec.  It is expecting exec to start a new chain builder, but the '.' says its adding onto a chain.

leela krishna

unread,
Aug 8, 2017, 4:02:24 PM8/8/17
to Gatling User Group
Thanks, Rob. The error was gone but, I'm not seeing the value of the "${value}" parameter. Its just printing ${value}

Robert Bacon

unread,
Aug 8, 2017, 4:11:47 PM8/8/17
to Gatling User Group
Oh, I think you don't need the quotes.  Try setting val x = ${value}.  I think that should work.  Here is hoping.

leela krishna

unread,
Aug 8, 2017, 4:16:53 PM8/8/17
to Gatling User Group
ah No. it says 

not found: value $

not found: value value

Robert Bacon

unread,
Aug 8, 2017, 4:36:59 PM8/8/17
to Gatling User Group
Are both of your objects under the same class?  Have you tried setting val x = value without any of the extra symbols.  Don't think that will work, but I'm trying to figure out the logic behind this.  My thoughts are either that or maybe single quotes 'value'.

leela krishna

unread,
Aug 9, 2017, 9:08:31 AM8/9/17
to Gatling User Group

Yes, both are in the same class. I am not sure why its not working for me :(

Robert Bacon

unread,
Aug 9, 2017, 11:18:25 AM8/9/17
to Gatling User Group
Ok Leela, I have a couple more ideas.  Let's try this...

Try calling your value like this:

val x: String = "${value}"

or like this:

val x = s"${value}"

let me know how that goes.

leela krishna

unread,
Aug 9, 2017, 11:46:49 AM8/9/17
to Gatling User Group
thanks for the response Rob, I tried both earlier but none of them worked
when

exec(session=>session.set("value",session("authHeader").as[String]))
.exec(test.dummy)

object test{
def dummy: String = {

val x: String = "${value}"
println(x)
return x
}

I see this 

TestScript is the only simulation, executing it.

${value}




When  I use like this
exec(session=>session.set("value",session("authHeader").as[String]))
.exec(test.dummy)

object test{
def dummy: String = {
val x = s"${value}"
println(x)
return x
}

I see something like this,

not found: value value

11:43:00.080 [ERROR] i.g.c.ZincCompiler$ -       val x = s"${value}"


I'm lot of ways but none of them are working :|

Robert Bacon

unread,
Aug 9, 2017, 1:30:54 PM8/9/17
to Gatling User Group
Ok Leela... I think I might have found the answer.  When calling your "value" for 'x', surround it with 3 additional quotes, so it will look like this:

val x = """"${value}""""

Let me know.

leela krishna

unread,
Aug 9, 2017, 2:05:41 PM8/9/17
to Gatling User Group
I can see the value stored in the variable by using something like this 
@volatile var val1 = ""
.exec(session=>{
val1 = session("authHeader").as[String]
println(val1)
session
})
so, when I print val1, I can see the value. But when I try to use that in a separate function, Its again printing ${value}


I even Tried your suggestion. Like this ...

 exec(session=>session.set("value",session("authHeader").as[String]))
  exec(test.dummy)


object test{
def dummy: String = {
    val x = """"${value}""""
println(x)
return x
}

}

Its just printing "${value}"

Robert Bacon

unread,
Aug 9, 2017, 2:11:19 PM8/9/17
to Gatling User Group
Just making sure I saw correctly, in other parts of your code, you actually used .saveAs("value"), correct?

leela krishna

unread,
Aug 9, 2017, 2:16:07 PM8/9/17
to Gatling User Group
yes. I'm using the same name as I used for saveAs.

Robert Bacon

unread,
Aug 9, 2017, 3:24:03 PM8/9/17
to Gatling User Group
Maybe that is confusing the compiler?  Have you tried taking out the session.set portion?

leela krishna

unread,
Aug 9, 2017, 3:53:53 PM8/9/17
to Gatling User Group
I tried multiple ways. How can we actually use the value which is saved using .saveAs("dummy")
in another function or request. Do you have any idea about that? 

Robert Bacon

unread,
Aug 9, 2017, 4:07:51 PM8/9/17
to Gatling User Group
All I know is that I was struggling with it for a long time, but finally got it to work.  Only problem is that the minor variances on your code and mine has extinguished any ideas I have as to why mine works and yours doesn't.  Everything else pretty much looks the same...

leela krishna

unread,
Aug 9, 2017, 4:16:52 PM8/9/17
to Gatling User Group
I'm using Intelli J as my IDE. Do you import any libraries to your project?

Stéphane LANDELLE

unread,
Aug 9, 2017, 4:25:44 PM8/9/17
to gat...@googlegroups.com
http://gatling.io/docs/current/session/expression_el/#expression-language

Warning

This Expression Language only works on the final value that is passed to the DSL method when the Simulation is instantiated.

For example, queryParam("latitude", "${latitude}".toInt + 24) won’t work, the program will blow on "${latitude}".toInt as this String can’t be parsed into an Int.

The solution here would be to pass a function:

session => session("latitude").validate[Int].map(i => i + 24).



Stéphane Landelle
GatlingCorp CEO


--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

leela krishna

unread,
Aug 9, 2017, 4:36:11 PM8/9/17
to Gatling User Group
I'm trying to get the session value Like this ...

  .exec(session=>session.set("value",session("authHeader").as[String]))


.exec(test.dummy)

object test{
def dummy: String = {
    val x: String="${value}"
println(x)
return x
}

}

But I'm just seeing this in my console

Device is the only simulation, executing it.

${value}

Simulation Device started...

16:33:04.728 [ERROR] i.g.c.a.b.SessionHookBuilder$$anon$1 - 'hook-1' failed to execute: Can't cast value 15dc8b3d3c7+13ec0afafvdfdfgddfgf0ddf84682b78e of type class java.lang.String into class io.gatling.core.session.Session


I've imported import io.gatling.core.Predef._
But still, I can't grab the session value

To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.

leela krishna

unread,
Aug 10, 2017, 10:53:56 AM8/10/17
to Gatling User Group
Hey Rob, Stephane, 
so, I'm saving my value as 
val scn = scenario("Device")
.feed(csvFeeeder)
.exec(http("Auth Header generation")
.post("/auth")
.headers(headers_0)
.formParam("type", "device")
.check(headerRegex("Auth","Device*").saveAs("authHeader"))

So, I'm trying to use that authHeader value in my second request. For that I'm trying to use the session to get the value. But I failed to get that. 
Please help me with this.

Robert Bacon

unread,
Aug 10, 2017, 2:30:34 PM8/10/17
to Gatling User Group
Leela,

When you call authHeader, did you use the session => session("authHeader").validate[String] syntax?

Potentially try:

session =>{
session("authHeader").validate[String]
session}

leela krishna

unread,
Aug 10, 2017, 3:36:22 PM8/10/17
to Gatling User Group
I did not try that. But where do I need to use that? 

Robert Bacon

unread,
Aug 10, 2017, 4:11:00 PM8/10/17
to Gatling User Group
That would be used where you try to call authHeader after you have used .saveAs

leela krishna

unread,
Aug 11, 2017, 9:36:26 AM8/11/17
to Gatling User Group
I used that, But I don't understand, how can I fetch the session value to my next .exec request. I think that session block will just validate my .saveAs. Correct me if I'm wrong Rob. I could not find a way to use my .saveAs value in my second request.

leela krishna

unread,
Aug 11, 2017, 9:38:11 AM8/11/17
to Gatling User Group
or Do you want me to assign this block 

session =>{
session("authHeader").validate[String]
session}

to any kind val?


On Thursday, 10 August 2017 16:11:00 UTC-4, Robert Bacon wrote:

leela krishna

unread,
Aug 14, 2017, 11:35:22 AM8/14/17
to Gatling User Group
I'm still unable to parse the session value stored in one request into other. 
I've two http requests
val scn = scenario("Device")
.feed(csvFeeeder)
.exec(http("Auth Header gene)

.post("/auth")
.headers(headers_0)
.formParam("type", "device")
.check(headerRegex("auth","device*").saveAs("authH"))
.check(status.is(401)))

.exec(http("Signed Req")
.post("/auth/oauth")
.headers(Map(
"x-id" -> "${id}",
"x-serial" -> "${serial}",
"Authorization" -> test.dummy))
.formParam("type", "device"))

the test.dummy is a function which converts the saved string "authH" into some format and that converted output needs to be passed to the "Authorozation" header in my second request.  for that purpose I'm using something like this

object test{
def dummy: String = {
val = (Here I need to get the saved authH session attribute)
--- logic to convert into my required format--
return convertedString
}
}


I was unable to get the sessionAttribute there. Is there  a way to get the sessionAttribute. Please HELP. I go through the gatling documentation but could not find any solution
Reply all
Reply to author
Forward
0 new messages