How can I use session variable inside jsonpath ?

1,345 views
Skip to first unread message

dg dg10

unread,
Jul 19, 2017, 9:37:22 AM7/19/17
to Gatling User Group
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
.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 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)


---------------------------------------------------------------------------
When I tried with "test" variable then it's working but with session("qaservice")  it's giving me error.

Thanks in advance!

dg dg10

unread,
Jul 19, 2017, 10:06:15 AM7/19/17
to Gatling User Group
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. 

dg dg10

unread,
Jul 21, 2017, 4:52:49 AM7/21/17
to Gatling User Group
Any pointer on above will be helpful.  


On Wednesday, July 19, 2017 at 7:07:22 PM UTC+5:30, dg dg10 wrote:

Barry Perez

unread,
Jul 21, 2017, 5:12:42 AM7/21/17
to Gatling User Group
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

dg dg10

unread,
Jul 21, 2017, 8:05:49 AM7/21/17
to Gatling User Group
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 ? . 

Barry Perez

unread,
Jul 21, 2017, 9:35:42 AM7/21/17
to Gatling User Group

Vu Nguyen

unread,
Jul 21, 2017, 10:57:59 PM7/21/17
to Gatling User Group

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

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!

dg dg10

unread,
Jul 24, 2017, 1:31:59 AM7/24/17
to Gatling User Group
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.
Reply all
Reply to author
Forward
0 new messages