I've got scenario that I send GET request which sometimes can return 404 and I don't want to report it as an error on the final reports. Additionally if that request is successful I need to extract multiple values from json response.
Generally those 2 actions I want to use work for me fine when used independent i.e.
extract multiple json values from response:
jsonPath("$..usrn").saveAs("usrn"),
jsonPath("$..street_descriptor").saveAs("street_descriptor"),
....
)
Handle 404s:
status.saveAs("responseStatus"),
checkIf(session => session("responseStatus").as[Int] == 200) {
substring(usualHeader).exists
},
checkIf(session => session("responseStatus").as[Int] == 404) {
substring(errorHeader).exists
}
But when I tried to combine those two actions what I was able to do, which could compile and actually extract data, was following code. But I feel it could be done without such duplication
.check(
status.saveAs("responseStatus"),
checkIf(session => session("responseStatus").as[Int] == 200) {
jsonPath("$..usrn").saveAs("usrn")
},
checkIf(session => session("responseStatus").as[Int] == 200) {
jsonPath("$..street_descriptor").saveAs("street_descriptor")
},
...
checkIf(session => session("responseStatus").as[Int] == 404) {
substring("404 error").exists
}
I'm on Gatling 3.0.3
you could use optional checks
.check(
status.in(200, 404),
jsonPath("$..usrn").optional.saveAs("usrn"),
jsonPath("$..street_descriptor").optional.saveAs("street_descriptor")
)