not able to save session variables in csv file

490 views
Skip to first unread message

Siddhi Kalamakar

unread,
Aug 22, 2016, 9:21:46 PM8/22/16
to Gatling User Group, slan...@gatling.io
Hello Team,

I have a JSON response with multiple values [say, IDs] in it, I want to extract all of them from the JSON response and save it in the .csv/feeder so that i can use them in th enest scenarios.

Json Response:
[
  {
    "id": 5156,
    "orderId": 1,
    "step": "Step Desc",
    "result": "Result Desc"
  },
  {
    "id": 5157,
    "orderId": 2,
    "step": "Step Desc",
    "result": "Result Desc"
  },
  {
    "id": 5158,
    "orderId": 3,
    "step": "Step Desc",
    "result": "Result Desc"
  }
]

I want to save the highlighted values in the csv file and my feeder should look like
id
5156,5157,5158

Till now i have used below methods
.check(jsonPath("$..id").findAll.saveAs("ID")
.exec( session =>
       {scala.tools.nsc.io.File("<FilePath>\\ID.csv").appendAll("ID"+",")
        session}
 )

However only first value is getting saved in the file and rest all getting ignored.
Kindly help to sort this issue.


Thanks in advance..!!

Siddhi Kalamakar

unread,
Aug 24, 2016, 1:54:39 AM8/24/16
to Gatling User Group, slan...@gatling.io
Hello Team,
Waiting for your response.

Your help would be much appreciated.

Thanks in advance..!!

Kind Regards,
Siddhi

CyberNinja

unread,
Aug 24, 2016, 10:38:10 PM8/24/16
to Gatling User Group, slan...@gatling.io
findAll will save the values in an array. So you will need to loop over ID[i] and write that to the file in the session.

See example here --> http://gatling.io/docs/2.2.2/http/http_check.html?highlight=saveas#http-response-body

Siddhi Kalamakar

unread,
Aug 25, 2016, 10:24:19 PM8/25/16
to Gatling User Group, slan...@gatling.io
Hello Team,

I tried using foreach, mkstring etc functions to extract the list of ID's from the session however only first ID is getting saved and the findAll it not able to extract the next ID in Session body.

My result is getting printed as
<first Id> viz 5156

There are no IDs which are getting fetched by findAll.

Nikolay Degkin

unread,
Sep 6, 2016, 11:16:08 AM9/6/16
to Gatling User Group, slan...@gatling.io
Hi Siddhi,

you can try something like this:

val scn = scenario("Siddhi")
 
.exec(http("GET")
 
.get("/my_way_to_your_JSON")
 
.check(jsonPath("$..id").findAll.saveAs("IdList"))
 
)
 
 
.foreach("${IdList}", "id") {
 
exec(session => {
 println
(session("id").as[String])
 session
})
 
}
In my example i print the iDs, you should change print to write to file

Siddhi Kalamakar

unread,
Sep 12, 2016, 7:03:28 AM9/12/16
to Gatling User Group, slan...@gatling.io
all IDs can be extracted by using below JSONPath expression
.check(jsonPath("$..[:].id").findAll.saveAs("IdList"))

After this save all the IDs in Vector and write that vector in csv file/feeder.
Message has been deleted
Message has been deleted
Message has been deleted

Nikolay Degkin

unread,
Sep 12, 2016, 8:24:34 AM9/12/16
to Gatling User Group, slan...@gatling.io
val writer = {
    val fos
= new java.io.FileOutputStream("foo.txt")
   
new java.io.PrintWriter(fos,true)
 
}
 
 val scn = scenario("FK")
 
.exec(http("Get")

 
.get("/my_path")

 
.check(jsonPath("$..id").findAll.saveAs("IdList"))
 
)
 
 
.foreach("${IdList}", "id") {
 
exec(session => {

 writer
.println(session("id").as[String])
 session
})
 
}

Reply all
Reply to author
Forward
0 new messages