How to use Array length to iterate/loop a post request and pass sequential array indexed value ? in scala-gatling

How to use Array length to loop a post request with passing sequential array indexed value ?

    .exec(http("testing")
      .post("/testUrl")
      .formParam("testvalue", "Testalert")
      .check(jsonPath("$[*].id").findAll.saveAs("TestID"))) // datatype : vector string / single dimensional array

I have to iterate the below request with testID.length and pass the array indexed value (TestID[i] to the json page

repeat(testID.length) {
    .exec(
      http("ViewTestPage")
        .post(TestUrl + "/graphql")
        .body(ElFileBody("testpage/testPageView.json")) // pass value TestID[i] , i++ at each iteration
        .check(status.is(200)))
}

for example :
testPageView.json file contains
{"query":"query{api{test(limit:${TestID[i]}){edges{node{id type{role}}}}\"}

Kindly suggest a solution

Hi @RudraGanesh,


import static io.gatling.javaapi.core.CoreDsl.*;
import static io.gatling.javaapi.http.HttpDsl.*;

import io.gatling.javaapi.core.*;
import io.gatling.javaapi.http.*;

public class ArraySimulation extends Simulation {

    HttpProtocolBuilder httpProtocol =
            http
                    .baseUrl("https://postman-echo.com");

    ScenarioBuilder scn =
            scenario("ArrayScenario")
                    .exec(
                            http("RetrieveAnArray")
                                    .post("/post")
                                    .body(StringBody("{\"values\":[{\"id\":\"one\"}, {\"id\":\"two\"}, {\"id\":\"three\"}]}"))
                                    .asJson()
                                    .check(jsonPath("$.json.values[*].id").findAll().saveAs("TestID")) // 1
                    ).foreach("#{TestID}", "currentID").on( // 2
                            exec(
                                    http("Get with the value")
                                            .get("/get")
                                            .queryParam("id", "#{currentID}") // 3
                                            .check(jsonPath("$.args.id").isEL("#{currentID}"))
                            ));

    {
        setUp(scn.injectOpen(atOnceUsers(1)).protocols(httpProtocol));
    }
}

Note: Here is a real SSCCE, you can run it without issue yourself.

  1. I had to change the jsonPath because postman-echo changes it.
  2. foreach is usually the way to do. Note that the “currentID” is the name of the session variable to store for each value in the list.
  3. I use the currentID value as you can use it in your testpage/testPageView.json file

Cheers!

2 Likes

@sbrevet Thank you very much
Yes, your solution worked :slight_smile: