简体   繁体   中英

How to use saved variable values outside of gatling scenario in scala file

One of gatling get request is giving multiple string values and I am saving them using saveAs like this:

val scn = scenario("ReadLogs")
        .exec(http("logEvent")
        .get("""/xyz/abc""")
        .check(jsonPath("$.data[*].message").findAll.saveAs("mList")))

/* My scala code to achieve some requirements*/

I can see in log that "mList" is a vector which have my required string messages. I want to process those messages in my scala code. How to do that in simple way? I think if i can use "mList" variable outside scn scenario than things will move fine so i'll put this question more specific. How can i use "mList" variable in my scala code?

Coding the process logic in a separate execution step, and make sure it is executed after the data is fetched.

val fetchLogs = exec(
  http("logEvent")
    .get("""/xyz/abc""")
    .check(jsonPath("$.data[*].message")
    .findAll
    .saveAs("mList")
)

val processLogs = exec { s: Session =>
    val mList = s("mList").as[Seq[Any]]
    val result = ...
    s.set("processResult", result)
}

val scn = scenario("ReadLogs").exec(
  fetchLogs,
  processLogs
)

Update: Save the data for later process

var mList: Seq[String] = _    

val fetchLogs = exec(
  http("logEvent")
    .get("""/xyz/abc""")
    .check(jsonPath("$.data[*].message")
    .findAll
    .transform { v => mList = v; v } // save the data
    .saveAs("mList")
)

val scn = scenario("ReadLogs").exec(fetchLogs)

after {
  // Process the data here. It will be executed when the simulation is finished.
}

There is a great Blog describing this: https://devqa.io/gatling-save-response-body/

What I missed in @thirstycrow's answer was this:

We can then reference the variable using ${tokenId}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM