简体   繁体   中英

Loop over multiple response matches from previous request in Gatling / Scala

I'm still pretty new to Gatling / Scala, so my apologies if I've misunderstood something obvious, but...

I have a scenario with a sequence of requests. One of them is along the lines of:

.exec (
  http("Get IDs")
  .post(<urlpath>)
  .body(<body text>)
  .headers(<headerinfo>)
  .check(jsonPath("$[*].some.json.path").findAll.transform(_.map(_.replace("unwantedchars,""))).saveAs(myIds)
)

And that works fine, returning a vector of all matching json elements, with the unwanted chars removed. What I'm trying to do next is loop over the first 5 ids, and pass them into the next request. I've tried assorted variants of the following, but no amount of variation / googling has returned the actual solution:

.exec( session => {
  val firstFive = session("myIds").as[Vector[String]].toArray.take(5)
  for (inx <- 0 until 4){
    exec(
      http("get the item")
        .get("/path/to/item/thing/" + firstFive(inx))
        .headers(<etc etc>)
      )
  session
})

So I understand nested exec's aren't supported - but how can I create a block that combines the for-loop, and the actual HTTP requests?

Am I missing something obvious?

if you're looking to take 5, then you just need to put them in the session and then process them with a .foreach block.

so instead of taking the first five and trying to make exec calls (which, as you observed, won't work), save the list back to the session

.exec( session => {
  val firstFive = session("myIds").as[Vector[String]].take(5)
  session.set("firstFive", firstFive)
)

then use the DSL to loop through the collection

.foreach("${firstFive}", "id") {
  exec(
    http("get the item")
      .get("/path/to/item/thing/${id}")
      .headers(<etc etc>)
  )
}

you could even add the selection of the first five to your transform step - so the whole thing could be...

.exec (
  http("Get IDs")
    .post(<urlpath>)
    .body(<body text>)
    .headers(<headerinfo>) .check(jsonPath("$[*].some.json.path").findAll.transform(_.map(_.replace("unwantedchars,"")).take(5).saveAs(myIds)
)
.foreach("${myIds}", "id") {
  exec(
    http("get the item")
      .get("/path/to/item/thing/${id}")
      .headers(<etc etc>)
  )
}

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