Pandi
May 30, 2014, 7:20pm
1
I am trying to use the value returned in a SOAP response and use it in the subsequent POSTs.
import io.gatling.core.Predef._
import io.gatling.core.session.Expression
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
import io.gatling.http.Headers.Names._
import io.gatling.http.Headers.Values._
import scala.concurrent.duration._
import bootstrap._
import assertions._
class GetPriceDetail extends Simulation {
//removed code…
.exec(
http(“LogSession”)
.post(“http://IPAddress/service.svc ”)
.headers(loginHeader)
.body(StringBody(soapLoginReq))
//.check(status.is(200))
.check(xpath("//SessionService:SessionToken", List(“SessionService” → “http://schemas.datacontract.org/2014/services.Contract.SessionService")).saveAs("sessionTkn ”))
)
println(s"Session Token: " + “$sessionTkn” )
}
With TRACE turned on, I can see the session token is received, but println() does not print the value, instead it prints “$sessionTkn”.
I am using Gatling 2.0 M3a.
How can I ‘store’ and ‘use’ the value sessionTkn in a subsequent call to POST?
Any help is greatly appreciated.
Thanks,
Pandi
Not looking too deeply into it but it looks like you have your println incorrect. Here is what I am guessing you wanted:
println(s"Session Token: $sessionTkn")
The s"" makes it an interpolated string (http://docs.scala-lang.org/overviews/core/string-interpolation.html ) so you don’t need to concat the value to the end.
Pandi
May 30, 2014, 8:37pm
3
Shawn,
Thanks for the response. If I use
println("Session Token: “$sessionTkn”)
I get the below result:
Simulation M2MMC.GetDeviceInfo started…
Session Token: $sessionTkn
< deleted part of output >
Your println() function isn’t called in a Gatling “context”.
For doing so, you have to encapsulate it in an .exec() call like this:
.exec( session =>println("Session Token: " + session(“sessionTkn”))
session
)
Notice that .exec() takes a Function[Session, Session] as argument and that the session is immutable.
regards
Nicolas
Pandi
May 30, 2014, 10:57pm
5
All, Thanks again for reading and helping.
After much researching the Forum and Gatling 2 documentation, I think, I got it working, at least I can see the correct “sessionTkn” value printed after the first POST-followed-by-saveAs. Hopefully the code example will help other newbies like me.
import io.gatling.core.Predef._
import io.gatling.core.session.Expression
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
import io.gatling.http.Headers.Names._
import io.gatling.http.Headers.Values._
import scala.concurrent.duration._
import bootstrap._
import assertions._
class GetPriceDetail extends Simulation {
val scn = scenario(“LoginAndDevInfo”)
.exec(
session => {
session.set(“sessionTkn”, “dummyValue”)
println("Session information: " + session) //for debug #1
session
}
)
//removed code…
.exec(
http(“LogSession”)
.post(“http://IPAddress/service.svc ”)
.headers(loginHeader)
.body(StringBody(soapLoginReq))
//.check(status.is(200))
.check(xpath("//SessionService:SessionToken", List(“SessionService” → “http://schemas.datacontract.org/2014/services.Contract.SessionService")).saveAs("sessionTkn ”))
)
.exec(session =>{
println(“Debug print#2”)
println(session(“sessionTkn”).as[String]) <<< this one prints the correct value for sessionTkn from the previous POST
session
})
setUp(scn.inject(ramp(1 users) over (10 seconds)))
}
Thanks
Pandi