...

/

Write your First Gatling Test Script Programmatically

Write your First Gatling Test Script Programmatically

Write your first Gatling test script.

In the previous lesson, we learned how to create a basic project structure and the necessary dependencies. In this lesson, we will write our first Gatling test script.

Gatling test scripts need to be written in Scala and are quite simple. However, for writing any utility functions, it may require more knowledge of Scala. We highly recommend learning Scala in depth if this is required for your uses and projects.

If you want to learn Scala, take this free beginner’s course on Educative’s platform.

Sample Gatling test script

In the sample Gatling test script, we will create two scenarios for running HTTP requests: one containing GET and the other containing POST. In both scenarios, we will perform some assertions as well.

Press + to interact
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
class SampleSimulation extends Simulation {
val httpProtocol = http
.baseUrl("http://localhost:8080")
.acceptHeader("*/*")
.doNotTrackHeader("1")
.userAgentHeader(
"Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0"
)
.disableWarmUp
.disableCaching
val getScenario = scenario("BasicSimulation - GET")
.exec(
http("GET request")
.get("/")
.check(status.is(200))
)
val postScenario = scenario("BasicSimulation - POST")
.exec(
http("POST request")
.post("https://reqres.in/api/register")
.body(StringBody("""{ "email": "eve.holt@reqres.in", "password": "pistol" }"""))
.asJson
.check(
status.is(200),
jsonPath("$.id").exists,
jsonPath("$.id").find.is("4")
)
)
setUp(
getScenario.inject(atOnceUsers(1)),
postScenario.inject(atOnceUsers(1))
).protocols(httpProtocol)
}

Understanding the code

  • All Gatling simulation classes need to extend io.gatling.core.scenario.Simulation

  • httpProtocol defines the global configuration for all the HTTP calls.

    • baseUrl – sets the default base URL for all the requests. It can be overridden for individual HTTP requests.
...