If you probably know a bit of Gatling. To show the advanced features of Gatling, we will continue to improve the scenario we developed.
Checks
Checks are an important feature of Gatling,Checking http response code.
.exec(http("request_1")
.get("/")
.headers(headers_1)
.check(status.is(302)) // This is our check!
)
.exec(http("request_1")
.get("/")
.headers(headers_1)
.check(status.is(302)) // This is our check!
)
Checking page loaded properly or not,
.exec(
http("request_4")
.get("/private/bank/accounts.html")
.headers(headers_4)
.check(regex("""<td class="gfty">ABC${account_id}</td>""").exists)
)
Here, we verify that the string """<td class="gfty">ABC${account_id}</td>""" is present in the body of the response we received.
Saving data
Checks can also be used for saving the extracted data so that it can be reused further in the scenario flow.
For example, one can use a capture group on a regex:
check(
regex("""<a href="/excil-google-web/account/(ACC[0-9]*)/home.html">""")
.saveAs("acc1"))
Here, if the regex matches the response body, the captured group first occurrence will be saved in the user’s session under the acc1 key.
Conditional Execution
You might have to execute different actions depending on a value in the session; to do so, you can use conditional execution. As an example, we could simulate that user7 is the only one to click on the logout button:
.doIf("${username}", "user7") {
exec(
http("request_9")
...
)
.pause(0 milliseconds, 100 milliseconds)
.exec(
http("request_10")
...
)
}
Using Scala Functions
doIf and asLongAs condition can be expressed using two strings that will be tested for equality (as shown in the previous example). It can also be expressed as a Scala Function of type Session => Boolean.
To illustrate this, we can simulate the opposite of the previous example; that is to say every user will click on logout except user7:
.doIf(session => session.getAttribute("username") != "user7") {
exec
...
}
Multi-Scenarios Simulations
What if your web application is used by different kind of users? You might have administrators, users, advanced users, etc. We can easily simulate the use of the application by each of these groups; with Gatling, you can even simulate the use of the application by all these groups at the same time.
To do so, just define another scenario in your simulation file:
val scn = scenario("Scenario Name")
...
val otherScn = scenario("Other Scenario Name")
...
There you go, you have defined several scenarios in one simulation file. If you try to run this simulation like this, you’ll notice that Gatling won’t see the difference; indeed, you need to add this scenario to the List of scenarios to be simulated:
setUp(
scn.users(10).ramp(10).protocolConfig(httpConf),
otherScn.users(5).ramp(20).protocolConfig(httpConf)
)
Note
As you can see, the httpConf can be reused for several scenarios.
If you want to delay the beginning of a scenario, you can use the method delay(duration: Int):
otherScn.users(5).ramp(20).delay(30).protocolConfig(httpConf)
With this configuration otherScn will start 30 seconds after scn.