How to create basic pagination in Gatling?

So I am trying to create basic pagination in Gatling but failing miserably.

My current scenario is as follows:

  1. First call is always a POST request, the body includes page index 1 and page size 50

I am then receiving 50 object + the total count of objects on the environment:


Since I need to iterate through all objects on the environment, I will need to POST this call 5 time, each time with an updated pageIndex.

My current (failing) code looks like:

  def getAndPaginate(jsonBody: String) = {
    val pageSize = 50;
    var totalCount: Int = 0
    var currentPage: Int = 1
    var totalPages: Int =0
    exec(session => session.set("pageIndex", currentPage))
    exec(http("Get page")
      .body(ElFileBody("json/" + jsonBody)).asJson
          .check(jsonPath("$.result.objects[?(@.type == 'type')].id").findAll.saveAs("list")))
      .exec(session => {
        totalCount = session("totalCount").as[Int]
        totalPages =  Math.ceil(totalCount/pageSize).toInt
      .asLongAs(currentPage <= totalPages)
        exec(http("Get assets action list")
          .body(ElFileBody("json/" + jsonBody)).asJson
          .check(jsonPath("$.result.objects[?(@.type == 'type')].id").findAll.saveAs("list")))
        currentPage = currentPage+1
        exec(session => session.set("pageIndex", currentPage))
        pause(Config.minDelayValue seconds, Config.maxDelayValue seconds)


Currently the pagination values are not assign to the variables that I have created at the beginning of the function, if I create the variables at the Object level then they are assigned but in a manner which I dont understand. For example the result of Math.ceil(totalCount/pageSize).toInt is 4 while it should be 5. (It is 5 if I execute it in the immediate window… I dont get it ). I would than expect asLongAs(currentPage <= totalPages) to repeat 5 times but it only repeats twice.

I tried to create the function in a class rather than an Object because as far as I understand there is only one Object. (To prevent multiple users accessing the same variable I also ran only one user with the same result) I am obviously missing something basic here (new to Gatling and Scala) so any help would be highly appreciated :slight_smile:

Don’t use global variables.
Store user data in user session.