Is it possible to pass data from a session in the ScenarioBuilder to a ChainBuilder in another class?

I have been instructed to create performance tests, and for this we have chosen Gatling (Java). I am however struggling with passing data. The application is quite complex, and I need to pass data. I swear I have seen it working, but there is no proof of that. I have the following code (snippets).

Here some code from the Scenario Class:

private final String environment = System.getProperty("environment", "local").toLowerCase();
private final String hostname = ConfigFactory.load().getString(environment + "Hostname");
private final String username = ConfigFactory.load().getString(environment + "Username");
private final String password = ConfigFactory.load().getString(environment + "Password");
private static final String uri2 = "https://<link>";

[...]

ScenarioBuilder scn = scenario("CalendarScenario")
                .exec(session -> {
                    session.set("username", username)
                            .set("password", password)
                            .set("patientID64", ConfigFactory.load().getString(environment + "PatientID64"))
                            .set("afspraakSoortID", ConfigFactory.load().getString(environment + "AfspraakSoortID"))
                            .set("agendaPatientID", ConfigFactory.load().getString(environment + "PatientID"));
                    return session;
                })
                .exec(User.login)
[...]

And here the code from the User Class and login chainbuilder:

public class User {

    public static ChainBuilder login =
            exec(
                    http("Open application")
                            .post("/application/")
                            .resources(
                                    http("Login")
                                            .get("login.jsp")
                                    ,
                                    http("CalendarScenario_2:GET_https://<link_login>")
                                            .get("login.jsp")
                                    ,
                                    http("Get CSS styles")
                                            .get("/styles/style.css?version=")
                            )
            )
                    .pause(2)
                    .exec(http("Login Verify User")
                            .post("/loginVerifyUser")
                            .formParam("username", "${username}")
                    )
[...]

The ${username} ends up being undefined (checked with println).

I try to work with “page objects” (or whatever you want to call it), to cover the same parts of the application that are used in different user scenarios and reduce the amount of code.

Is it even possible to pass data from a session in a scenario to another class?

.exec(session -> {
    session.set("username", username)
        .set("password", password)
        .set("patientID64", ConfigFactory.load().getString(environment + "PatientID64"))
        .set("afspraakSoortID", ConfigFactory.load().getString(environment + "AfspraakSoortID"))
        .set("agendaPatientID", ConfigFactory.load().getString(environment + "PatientID"));
      return session;
})

From the official documentation: Gatling - Session API

Session instances are immutable, meaning that methods such as set return a new instance and leave the original instance unmodified!

1 Like

Thank you, slandelle. That was exactly what I needed. In hind side it is so simple (will be keeping the documentation open from now on).

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.