Gatling - Scala : How to repeat a request until a certain response variable exists in the API response?

Gatling - Scala : How to repeat a request until a certain response variable exists in the API response?

This is the request to find the response time of a cursor pagination API

.exec(http("APITests:Cursor Pagination")
  .get("/testapi")
  .queryParam("sortField", "ID")
  .queryParam("limit", limitCount)
  .queryParam("cursor", "#{CursorID}")
  .check(jsonPath("$.nextCursor")).exists
 .check(status.is(200))
)

I have to repeat the request execution until .check(jsonPath(“$.nextCursor”)).exists = False

Please provide suggestions and help

I tried below ending with error:

 doWhile(session => session(".check(jsonPath(\"$.nextCursor\").exists").as[Boolean]) {
    exec(http("APITests:Cursor Pagination")
      .get("/testapi")
      .queryParam("sortField", "ID")
      .queryParam("limit", limitCount)
      .queryParam("cursor", "#{CursorID}")
      .check(status.is(200))
      .check(jsonPath("$.nextCursor").exists
    ))
  }

But I end up with error :
jsonPath($.nextCursor).find.exists, found nothing

Hi @RudraGanesh,

It is not possible to use check outside the request scope.

You current real check (last line) can save the value in session. Then, you will be able to use that session variable.

 doWhile(session => session("nextCursorExist").as[Boolean]) {
    exec(http("APITests:Cursor Pagination")
      .get("/testapi")
      .queryParam("sortField", "ID")
      .queryParam("limit", limitCount)
      .queryParam("cursor", "#{CursorID}")
      .check(status.is(200))
      .check(jsonPath("$.nextCursor").exists.saveAs("nextCursorExist"))
    )
  }

Does that help?

Cheers!

1 Like

Hi @sbrevet ,

Thank you. yes it works.
but I am getting error like

Domains:Cursor Pagination: Failed to build request: No attribute named ‘CursorID’ is defined
jsonPath($.nextCursor).find.exists, found nothing

When the value is not found the loop should exit without throwing error message.
and how can I include variable from previous response to dowhile session request?
Please help me

I expect that you will use this nextCursor in the following request, do you?

 doWhile(session => session("CursorID").as[Option[String]].isDefined) {
    exec(http("APITests:Cursor Pagination")
      .get("/testapi")
      .queryParam("sortField", "ID")
      .queryParam("limit", limitCount)
      .queryParam("cursor", "#{CursorID}")
      .check(status.is(200))
      .check(jsonPath("$.nextCursor").optional.saveAs("CursorID"))
    )
  }

WDYT?

1 Like

Yes, Thank you for providing the solution
This solves my issues.