Search code examples
scalaperformance-testinggatlingscala-gatling

Add step to scenario after initialization in gatling


I'm new at scala and also gatling. I'm trying to create some sort of scenarioBuilder executing different steps based on a certain conditions but I have an issue. I don't know if I can add steps to my scenario. something like that

val scn = scenario("scenario")
    for(req <- requestsList.requests)
      if(req.method == "GET")
       scn.exec("do something")

It is possible to add steps in loop and also based on condition?


Solution

  • Convert your request list into a Feeder and then use doSwitch.

    Suppose your request are put into a .csv file with two columns:

    method, endpoint
    GET,/api/bar
    POST,/api/bar
    GET,/api/foo
    
    val requestFeeder = csv("requests.csv") 
    
    val scn: ScenarioBuilder =
        scenario("Foo Scenario")
          .feed(requestFeeder)
          .doSwitch("${method}")(
            "GET" -> exec(http("Get request")
              .get("${endpoint}")
              .check(status.in(200 to 210))),
            "POST" -> exec(http("Get request")
              .post("${endpoint}")
              .check(status.in(200 to 210))))