Run Java code in between simulation i.,e before or after a scenario

Here is my use case :

  1. Run Java Code to make a setup in AUT
  2. run a gatling scenario in a simulation
  3. run a java code to change setup in AUT
  4. run other gatling scenario in same simulation
  5. run other java code to again change setup in AUT
  6. run other gatling scenario
  7. finally generate a single report

currently the setup i have results in 3 reports as i am running 3 simulations after making AUT setup changes . as a result 3 separate reports are generated. is there any solution/approach for this problem? ie., logically they are supposed to run in same simulation and generate single report . but i donot see an option to run a java code post a gatling scenario before next scenario starts, hence i am doing it as 3 separate simulations which is not desired.

Hi!

Not sure what AUT means. Here what I understand of your use case:

You have a Java static method to call, let’s say:

package com.example.setup;

public class JavaCode {
    public static void setUpAUT() {
        /// implementation here
    }
}

You can call this code from a Simulation:

package computerdatabase;

import java.time.Duration;

import com.example.setup.JavaCode;
import io.gatling.javaapi.core.*;
import io.gatling.javaapi.http.*;

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

public class BasicSimulation extends Simulation {

  HttpProtocolBuilder httpProtocol = http
    .baseUrl("https://computer-database.gatling.io") 
    .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") // Here are the common headers
    .acceptEncodingHeader("gzip, deflate")
    .acceptLanguageHeader("en-US,en;q=0.5")
    .userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0");

  ScenarioBuilder setupBetween = scenario("Setup scenario") // <= Here an util scenario
          .exec(session -> {
            JavaCode.setUpAUT(); // <= to call your Java code
            return session;
          });

  ScenarioBuilder firstScenario = scenario("My super scenario")
          .exec(http("Home").get("/"));

  ScenarioBuilder secondScenario = scenario("My super scenario")
          .exec(http("Amiga").get("/computers/70"));


  {
    setUp(
            setupBetween.injectOpen(atOnceUsers(1)).andThen(
                    firstScenario.injectOpen(atOnceUsers(1)).andThen(
                            setupBetween.injectOpen(atOnceUsers(1)).andThen(
                                    firstScenario.injectOpen(atOnceUsers(1)).andThen(
                                            setupBetween.injectOpen(atOnceUsers(1)).andThen(
                                                    secondScenario.injectOpen(atOnceUsers(1))
                                            )
                                    )
                            )
                    )
            )
    ).protocols(httpProtocol);
  }
}

Note: I used Gatling Java API. But similar things works well from scala or kotlin.

Does that helps?

Cheers!

1 Like

AUT = Application Under Test

1 Like