问题
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
}
)
}
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?
回答1:
exists
must be replaced by optional
.check(jsonPath("$..user.rooms[0].id").optional.saveAs("roomId"))
来源:https://stackoverflow.com/questions/40717357/gatling-fetch-data-from-response-for-next-requests