加特林.从响应中获取下一个请求的数据 [英] Gatling. Fetch data from response for next requests

查看:53
本文介绍了加特林.从响应中获取下一个请求的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从JSON响应中获取可选数据以构建下一个请求.我使用check(jsonPath("...rooms[0].id").exists.saveAs("...roomId")),但是如果用户没有房间,则.check()会将结果解释为错误(例如request count 4 (OK=3 KO=1))

I need to fetch optional data from a JSON response to build next requests. I use check(jsonPath("...rooms[0].id").exists.saveAs("...roomId")) but if the user has no rooms, the result is interpreted by .check() as an error (e.g. request count 4 (OK=3 KO=1))

object Users {
  val execute = exec(http("Users"))
    .get("/api/user?userIdentifier=${userId}")
    // if user has no rooms, the check results in an error (KO) 
    .check(jsonPath("$..user.rooms[0].id").exists.saveAs("roomId"))
    .pause(1)
}

object Room {
  val execute = exec(http("Room"))
    .get("/api/room?id=${roomId}")
    .pause(1)
}

val readOnlyScenario = scenario("Read only scenario") {
  .feed(userIdsdata)
  .exec(Users.execute,
    doIf(session => session.contains("roomId")) {
      Room.execute
    }
  )
}

如何从JSON响应中获取可选数据并将其保存在会话中(如果存在),而当不存在数据时.check()不会失败?

How can I fetch optional data from a JSON response and save it in the session (if present) without .check() failing when the data is not present?

推荐答案

exists必须替换为optional

.check(jsonPath("$..user.rooms[0].id").optional.saveAs("roomId"))

这篇关于加特林.从响应中获取下一个请求的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆