How to check if an attribute is present in Gatling/Scala
Asked Answered
H

4

7

I would think this is probably easy to do, but I really don't know Scala at all.

I have a scenario where a first time user will log in and see a page, then if they log in again they won't see this page. So the best I've come up with so far is this:

val chain = exec(
http("Login page")
  .get("/en/login")
  .headers(Config.HTML_HEADER)
).exec(
      http("login request")
        .post("/en/j_spring_security_check")
        .formParam("j_username", """${username}""")
        .formParam("j_password", """${password}""")
        .check(status.is(200))
        .check(currentLocationRegex(".*termsAndConditions").optional.saveAs("tc"))
    )
    .doIf(session => !session("tc").equals(null)) { // this doesn't work 
      exec(AgreeTermsAndConditions.chain)
  }

So I've tried a bunch of things on the doIf, the goal is just do if session "tc" is not set. Is there an easy way to do that?

Hooknosed answered 19/3, 2015 at 10:11 Comment(0)
S
1

Properly read the documentation: ${tc.exists()}.

Support answered 19/3, 2015 at 12:34 Comment(2)
Thanks, I actually got it working doing session.conatins("tc"), but your way is better. I've updated that. I thought I had tried that actually, but guess I missed that in the docs.Hooknosed
The Gatling docs aren't the easiest to navigate and lack a lot of "big picture" context. A newcomer like myself would be hard pressed to know that the answer to a question about "doIf" lies in the "EL" page, and your answer doesn't provide that context either. Contrast your answer with Camilo Silva's to see what I mean. "Properly read the documentation" is unnecessarily confrontational.Taproot
U
3

Gatling provides an exists() built-in EL function (see Galting EL documentation), so a succincter solution would be:

.doIf("${tc.exists()}") {
 ...
}
Unlimited answered 22/4, 2020 at 14:8 Comment(0)
S
1

Properly read the documentation: ${tc.exists()}.

Support answered 19/3, 2015 at 12:34 Comment(2)
Thanks, I actually got it working doing session.conatins("tc"), but your way is better. I've updated that. I thought I had tried that actually, but guess I missed that in the docs.Hooknosed
The Gatling docs aren't the easiest to navigate and lack a lot of "big picture" context. A newcomer like myself would be hard pressed to know that the answer to a question about "doIf" lies in the "EL" page, and your answer doesn't provide that context either. Contrast your answer with Camilo Silva's to see what I mean. "Properly read the documentation" is unnecessarily confrontational.Taproot
R
1

Worked with:

.doIf(session => !session.contains("tc")) {
    ...
}
Roundtheclock answered 5/6, 2019 at 7:46 Comment(0)
E
-2

Check with Nil instead. It works for me.

Ell answered 16/6, 2017 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.