Hi all
I’m trying to chain two requests together, so that the second request depending on two values from the first request:
val UserSelect = exec(.set(“userId”, “0”))
.exec(
http(“GetUserById”)
.get("/" + Properties.INSTANCE + “/system/users?uid=${userId}”)
.headers(headersstuff)
.check(jsonPath("$…Users[0].itemId").dontValidate.saveAs(“itemId”))
.check(jsonPath("$…Users[0].isFlagged").dontValidate.saveAs(“isFlagged”)))
.exec(Pause.reqPause)
.doIf( .contains(“itemId”)) {
exec(http(“GetMoreUserData”)
.post("/" + Properties.INSTANCE + “/system/more?id=${itemId}”)
.headers(headers_loggedIn)
.header(“Content-Length”, “0”))
.exec(Pause.reqPause)
}
In the above snippet, the 2nd request will fire depending on the " .doIf(_.contains(“itemId”)" line.
I would like to extend that, so that the doIf checks the itemId and the boolean value of the isFlagged variable, essentially
.doIf(_.contains(“itemId”) && isFlagged=true)
Is there a way to do that?
Wayne
Have you tried chaining them?
.doIf(first Condition).doIf (Second Condition)
Also you can set a boolean value in session variable based on both the conditions and use that in .doIf.
I tried chaining them like this:
.doIf(_.contains(“itemId”)).doIf("${isFlagged}", “true”) {
which seemed to be OK from the Gatling documentation, but it seems to be a syntax error.
I guess I could set another variable in the session, but I was hoping there would be a simple way to chain the conditions.
I don’t have any elegant solution. You can also try .
`
doIf(Condition 1){
doIf(condtion 2) {
exec(…)
}
}
`
Thanks - this approach is working!
// => so isFlagged is a boolean and not a String (if it’s indeed a boolean value in JSON format)
// dontValidate is deprecated, use optional instead
jsonPath("$…Users[0].isFlagged").ofType[Boolean].optional.saveAs(“isFlagged”))
Then you can simply write doIf("${isFlagged}")
Is there a way to do more than a simple string comparison? We have a need to be able to do something like an OR in a test. Also, an ability to match/compare against integers.
I’m guessing it must be possible to get some Scala to do it, but we’re not having any luck.
Thanks
Excilys
December 18, 2014, 10:46am
8
Yep, easy.
But please provide an example of what you’re trying to do
OK, using my original example:
val UserSelect = exec(.set(“userId”, “0”))
.exec(
http(“GetUserById”)
.get("/" + Properties.INSTANCE + “/system/users?uid=${userId}”)
.headers(headersstuff)
.check(jsonPath("$…Users[0].itemId").dontValidate.saveAs(“itemId”))
.check(jsonPath("$…Users[0].itemCount").dontValidate.saveAs(“itemCount”))
.check(jsonPath("$…Users[0].isFlagged").dontValidate.saveAs(“isFlagged”)))
.exec(Pause.reqPause)
.doIf( .contains(“isFlagged”) OR ${itemCount} < 9) {
exec(http(“GetMoreUserData”)
.post("/" + Properties.INSTANCE + “/system/more?id=${itemId}”)
.headers(headers_loggedIn)
.header(“Content-Length”, “0”))
.exec(Pause.reqPause)
}
Essentially, I want to be able to do something like this:
.doIf(_.contains(“isFlagged”) OR ${itemCount} < 9) {
Thanks
Excilys
December 18, 2014, 3:42pm
10
.doIf(_.contains("isFlagged") OR ${itemCount} < 9) {
You can't use Gatling EL (which is based on parsed Strings, you can't write
${} in the middle of nowhere).
Please have a look at the documentation:
http://gatling.io/docs/2.1.1/session/expression_el.html
What you want is:
Assuming itemCount is always defined, like injected from a feeder:
doIf(session => session.contains("isFlagged") ||
session("itemCount").as[Int] < 9) {
Otherwise, the clause evaluation will crash and hurt performance, so this
is better:
doIf(session => session("itemCount").validate[Int].map(count => count < 9
&& session.contains("isFlagged"))) {
Thanks - I’ll give that a try. (My example wasn’t meant to be implemented, I was simply trying to show which variables I needed to compare!)
Cheers
Here it is trying to check both VIES and ALL… I want to execute statement if any one of this is true.
.doIf(session => session(“sFunctionality”).as[String].equals(“VIES”) || session(“sFunctionality”).as[String].equals(All))