Conditional checks

Hiya

I have a situation where I might receive a 200 or a 404 in a test. Both are valid, but in the case of the 200, I want to be able to save some parts of the body. Here is what I have currently:

val getPartial = exec(
http(“GetPartial”)
.get("/user/1/tran/final")
.header(“blah-blah”, “${val}”)
.check(status.in(Seq(200, 404)))
.check(jsonPath("$.pre[?(@.tval == ‘some’)].url").exisits.saveAs(“someUrl”))
.check(jsonPath("$.pre[?(@.tval == ‘all’)].url").exists.saveAs(“allUrl”)))

The problem with the above is that when the return code is 404, I don’t want to look for the json data. Is there a way to only do the subsequent checks if the return code is 200?

cheers
Wayne

You can use doIf conditions

http://gatling.io/docs/2.1.7/general/scenario.html?highlight=doif#doif

`

val getPartial = exec(
http(“GetPartial”)
.get("/user/1/tran/final")
.header(“blah-blah”, “${val}”)
.check(status.in(Seq(200, 404)).saveAs(“status”))
.doIf("${status}", 404){

check(jsonPath("$.pre[?(@.tval == ‘some’)].url").exisits.saveAs(“someUrl”))
.check(jsonPath("$.pre[?(@.tval == ‘all’)].url").exists.saveAs(“allUrl”)))
}

`

Thank you! That looks like exactly what I need.

cheers
Wayne

So I wrangled with this and no luck. The problem is that doIf() is used at the scenario level, and is not available when we get to looking at the results of a request.

The original problem remains.

cheers
Wayne

You can make the extract optional, and then it will not complain when the result is a 404. Sadly, it will also not complain if the result is a 200 but the result was not found. But then you could use a doIf to inspect that the result is what was inspected, and then error out if it is not.

Alternatively, you can store the status and the full json in a variable, and then do some logic using doif. However, then you would have to use a different method for parsing and extracting the parts you need from the json. I’ve done it. It’s doable. But it will not be done using basic Gatling DSL. You can use Jackson or Boon, but you will have to interface to those libraries yourself. It’s not a ton of code, but it is doable.

You have to write your own Check.