How can I use session variable inside jsonpath ?

Hi,
I need use session variable inside jsonpath but it’s giving me type -error. Looks like I need to tell jsonpath this is session variable, though I tried with “session =>” syntax but no luck.
Can someone please help me in this ?. Also , how can I convert my “session variable” to a variable, which can be used directly in jsonpath ?.

Code :

import scala.concurrent.duration._

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._

class RecordedSimulation extends Simulation {

val httpProtocol = http
.baseURL(“http://api.test.local”)
.inferHtmlResources()
.acceptHeader(“application/json, text/javascript, /; q=0.01”)
.acceptEncodingHeader(“gzip, deflate”)
.acceptLanguageHeader(“en-US,en;q=0.5”)
.userAgentHeader(“Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0”)

val uri1 = “http://api.test.local/api/v1/services
val count = new java.util.concurrent.atomic.AtomicInteger(1)
var test = "“platform-qa-test-service-1-0-0-test-1"”

val scn = scenario(“Test”)
.exec(session => session.set(“s_count”, count.getAndIncrement))
.exec(session => session.set(“qaservice”, “platform-qa-test-service-1-0-0-test-” + session(“s_count”).as[String]))
.exec(http(“GetService”)
.get(uri1)
.header(“Content-Type”, “application/json”)
.check(status is 200)
.check(jsonPath("$…[?(@.Permalink== " +test+ “)].ID”).saveAs(“loadID”)) // With test variable it’s working

setUp(scn.inject(rampUsers(2) over(20 seconds))).protocols(httpProtocol)

}

I forgot to mention that I can solved this by another way i.e using “(”$…[?(@.Permalink== “+”"" +test+count.getAndIncrement+"""+ “)].ID”)" but I would like understand expert solution on above problem.

Any pointer on above will be helpful.

Hi,

Check the documentation for Session API:

http://gatling.io/docs/current/session/session_api/

Specifically:

Warning

session("foo") doesn’t return the value, but a wrapper.

Cheers,
Barry

Thanks Barry for the reply. I tried “check(jsonPath(”$…[?(@.Permalink=="+""" + session(“qaservice”).asOption[String] +"""+ “)].ID”).saveAs(“reloadid”))) " earlier as well but it’s giving me error “not found: value session” . Looks like, JsonPath not identified the session variable. Also tried with “session =>” but still getting error. Does I missed something or any other way around to pass session variable inside jsonpath ? .

Hi,

OK - this might help:

http://gatling.io/docs/current/session/expression_el/#expression-language

Barry

My comment is likely a shot in the dark (since I haven’t implemented this):

This is my understanding of jsonPath from http://gatling.io/docs/current/http/http_check/#http-response-body:
jsonPath(expression) where expression can be a plain String, a String using Gatling EL or an Expression[String]

I believe you can try this:
`
jsonPath(session => session(“qaservice”).validate[String].map(value => s"""$…[?(@.Permalink=="${value}")].ID"""))

`

Hope that works!

Thanks Vu and Barry.

@Vu, yes you’re right, I tried with “.check(jsonPath( session => “$…[?(@.Permalink==”+ session(“qaservice”).as[String] + “)].ID”).saveAs(“ID”)))” and it’s working. Thanks again.