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
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
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.
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) ?
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)
Try taking out the ‘.’ before exec. It is expecting exec to start a new chain builder, but the ‘.’ says its adding onto a chain.
Thanks, Rob. The error was gone but, I’m not seeing the value of the “${value}” parameter. Its just printing ${value}
Oh, I think you don’t need the quotes. Try setting val x = ${value}. I think that should work. Here is hoping.
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’.
Yes, both are in the same class. I am not sure why its not working for me
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.
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
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.
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}”
Just making sure I saw correctly, in other parts of your code, you actually used .saveAs(“value”), correct?
yes. I’m using the same name as I used for saveAs.
Maybe that is confusing the compiler? Have you tried taking out the session.set portion?
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?
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…