Conditions
Learn about conditional statements that Gatling provides.
We'll cover the following...
In the previous lesson, we learned about the different loops that Gatling offers. In this lesson, we will learn about the different conditional statements offered by Gatling.
Gatling provides various conditional statements, such as doIf
, doIfEquals
, doIfOrElse
, doIfEqualsOrElse
, doSwitch
, doSwitchOrElse
, randomSwitch
, randomSwitchOrElse
, uniformRandomSwitch
, and roundRobinSwitch
. Let’s see them in more detail.
doIf
You can use the doIf
method for executing a specific chain of actions only when some condition is satisfied.
condition
will be looked up in the user session or any other value that evaluates to Boolean
.
doIf("${condition}") {
exec(...)
}
doIf(session => session("condition").as[Boolean]) {
exec(...)
}
A sample scenario:
import io.gatling.core.Predef._import io.gatling.http.Predef._class SampleSimulation extends Simulation {val protocols = http.disableCaching.disableWarmUp.baseUrl("http://localhost:8080")val scn = scenario("sample scenario").exec(session => session.set("condition", true)).doIf(session => session("condition").as[Boolean]) {exec(http("get info of user id 1").get("/api/users/1").check(status.is(200)))}setUp(scn.inject(atOnceUsers(1))).protocols(protocols)}
doIfEquals
We can use the doIfEquals
method for executing a specific chain of actions only when the given value equals the expected value.
doIfEquals("${actualValue}", "expectedValue") {
exec(...)
}
doIfEquals(session => session("actualValue"), session => session("expectedValue")) {
exec(...)
}
A sample scenario: