循环遍历 Gatling / Scala 中先前请求的多个响应匹配

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

我对 Gatling / Scala 还是很陌生,所以如果我误解了一些明显的东西,我深表歉意,但是...

我有一个包含一系列请求的场景。其中之一是:

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

这很好用,返回了一个包含所有匹配 json 元素的向量,删除了不需要的字符。接下来我要做的是遍历前 5 个 id,并将它们传递到下一个请求中。我尝试了以下各种变体,但没有多少变体/谷歌搜索返回实际解决方案:

.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
})

所以我知道嵌套的 exec 不受支持 - 但我如何创建一个块来组合 for 循环和实际的 HTTP 请求?

我是不是遗漏了什么明显的东西?

如果你想拿 5 个,那么你只需要将它们放在会话中,然后用 .foreach 块处理它们。

因此,与其获取前五个并尝试进行 exec 调用(正如您观察到的那样,这是行不通的),不如将列表保存回会话

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

然后使用 DSL 遍历集合

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

您甚至可以将前五个的选择添加到您的转换步骤中 - 这样整个事情就可以...

.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>)
  )
}