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

**URL:** <https://community.gatling.io/t/is-it-possible-to-pass-data-from-a-session-in-the-scenariobuilder-to-a-chainbuilder-in-another-class/7834>\
**Category:** Gatling (Open-Source)\
**Created:** [April 11, 2023, 8:36am UTC](https://community.gatling.io/t/is-it-possible-to-pass-data-from-a-session-in-the-scenariobuilder-to-a-chainbuilder-in-another-class/7834 "2023-04-11T08:36:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![promedico](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@promedico](https://community.gatling.io/u/promedico)\
**Post date:** [April 11, 2023, 8:36am UTC](https://community.gatling.io/t/is-it-possible-to-pass-data-from-a-session-in-the-scenariobuilder-to-a-chainbuilder-in-another-class/7834/1 "2023-04-11T08:36:40Z")

</div>

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** :

```auto
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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [April 11, 2023, 11:15am UTC](https://community.gatling.io/t/is-it-possible-to-pass-data-from-a-session-in-the-scenariobuilder-to-a-chainbuilder-in-another-class/7834/2 "2023-04-11T11:15:39Z")

</div>

```java
.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](https://gatling.io/docs/gatling/reference/current/core/session/session_api/#setting-attributes)

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

---

<div class="post-metadata">

**Author:** ![promedico](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@promedico](https://community.gatling.io/u/promedico)\
**Post date:** [April 12, 2023, 8:37am UTC](https://community.gatling.io/t/is-it-possible-to-pass-data-from-a-session-in-the-scenariobuilder-to-a-chainbuilder-in-another-class/7834/3 "2023-04-12T08:37:03Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/b/b148e0742de35c6280726cabf3ecd3de2d320f63.png) [@system](https://community.gatling.io/u/system)\
**Post date:** [May 12, 2023, 8:37am UTC](https://community.gatling.io/t/is-it-possible-to-pass-data-from-a-session-in-the-scenariobuilder-to-a-chainbuilder-in-another-class/7834/4 "2023-05-12T08:37:52Z")

</div>

Closed on May 12, 2023, 8:37am UTC

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