How to get access to gatling session var for use in other scala function

95 views
Skip to first unread message

Christian Crews

unread,
Aug 27, 2015, 2:22:43 PM8/27/15
to Gatling User Group
Hi, I'm trying to figure out how to get access to a session var for later use in a scala function. I've searched through the forum and I've tried the following with no success. What do I need to do?

  var mId = ""
   
  val scn
= scenario("ReadLogs")
   
.exec(http("Some Request")
     
.get("/somepath")
       
.check(jsonPath("$.id").findAll.saveAs("myId")))
   
.exec(session => {
      mId
= session.get("myId").as[String]
      println
("My Id: " + mId )
      session
   
})
 
 
def someFunction(): Unit = {
   
//do something with mId
 
}


Abhinav Gogna

unread,
Aug 27, 2015, 5:18:06 PM8/27/15
to Gatling User Group
What does the following print?
 println("My Id: " + mId )

if it is only My Id:  then your jsonPath is wrong. Test it here https://jsonpath.curiousconcept.com/

Christian Crews

unread,
Aug 27, 2015, 5:40:36 PM8/27/15
to Gatling User Group
Within the session block it correctly prints the id I expect. The var is getting saved to the session correctly, so that's not the issue. The issue is that outside of that block, in someFunction mId is back to being "". How can I access the value that I set within the block in a scala function?

Abhinav Gogna

unread,
Aug 27, 2015, 5:58:29 PM8/27/15
to Gatling User Group
You should be able to get the value by using mId, if you have declared it globally.

 def someFunction(): String = {
   
//do something with mId
    mId
= mId +" 2"
 
}


val scn = scenario("ReadLogs")
   
.exec(http("Some Request")
     
.get("/somepath")
       
.check(jsonPath("$.id").findAll.saveAs("myId")))
   
.exec(session => {
      mId
= session.get("myId").as[String]
      println
("My Id: " + mId )

      session
   
}).exec{ session =>
      println
(
someFunction)
session
}

Although vars are something you should avoid. May I ask what are you trying to do?

May I ask what you are trying to do in that function?
Reply all
Reply to author
Forward
0 new messages