CheckIf conditions

Hello!
My situatuion is quiet simple. I’ve got two menus on my site, one of them is avalaible for all users, and the other one only for admins. I have to collect all the links from the menu.
Haven’t find simple examples of using checkIf besides the Gatling documentation, so I’ve suggested to create it so as I used to in all my Java tests - to make a method with arguments.

def getMenu(menu_access: Boolean): ChainBuilder = {
exec(http("/user/${id}/first_page")
.get("/user/${id}/first_pages")
.headers(headers.headers_0)
.check(css("#block-kabinete-kab-links-1 ul a", “href”).findAll.saveAs(“links”))
.check(checkIf(menu_access)(css("#block-kabinete-kab-links-2 ul a", “href”).findAll.saveAs(“links2”))))
}

val client = scenario(“Clients”).exec(getMenu(menu_access = false))
val admin = scenario(“Admins”).exec(getMenu(menu_access = true))

But I’m getting two errors and don’t understand what is the problem even according to documentation:
checkIf(condition: Expression[Boolean])(thenCheck: Check)

inferred type arguments [io.gatling.core.check.CheckBuilder[io.gatling.http.check.HttpCheck,io.gatling.http.response.Response,jodd.lagarto.dom.NodeSelector,Seq[String]]] do not conform to method checkIf’s type parameter bounds [C <: io.gatling.core.check.Check[_]]

type mismatch;
[error] found : io.gatling.core.check.CheckBuilder[io.gatling.http.check.HttpCheck,io.gatling.http.response.Response,jodd.lagarto.dom.NodeSelector,Seq[String]]
[error] required: C
[error] .check(checkIf(menu_access)(css("#block-kabinete-kab-links-2 ul a", “href”).findAll.saveAs(“links2”))))

Hi,

checkIf has other signature, it takes Expression[Boolean] as a condition, and you are passing simple Boolean. Expression[Boolean] is an alias for a lambda: type Expression[T] = Session => Validation[T].

Here is a sample method that works in checkIf condition:

`
private val responseIsValid: (Response, Session) => Validation[Boolean] = {
(response: Response, _) => response.statusCode.contains(200)
}

`

And the usage:

http("[GET] Some request") .get("/some/path/${some_session_var}/") .check( checkIf(responseIsValid) { jsonPath("$.dummy[*].attr").findAll.optional.saveAs("some_name") } )

But you don’t have to use more complex checkIf for your case, you can simply conditionally add the second check to the http request, like this:

`
def getMenu(menu_access: Boolean): ChainBuilder = {
val baseRequest = http("/user/${id}/first_page")
.get("/user/${id}/first_pages")
.headers(headers.headers_0)
.check(css("#block-kabinete-kab-links-1 ul a", “href”).findAll.saveAs(“links”))

if (menu_access) {
exec(baseRequest.check(css("#block-kabinete-kab-links-2 ul a", “href”).findAll.saveAs(“links2”)))
} else {
exec(baseRequest)
}
}

`

Good luck!
Adam

Thank you for the explanation)