Gatling EL Not Working as expected

hihi,

I’m attempting to grab the email address from a CSV file using Gatling EL (#{email}) and use it to create a Cookie by retrieving the associated access-token from a static HashMap<String, String> . However I get:

[ERROR] i.g.a.Gatling$ - Run crashed
java.lang.NullPointerException: Cannot invoke “String.contains(java.lang.CharSequence)” because “raw” is null

Here is the code I’m using:

private ScenarioBuilder landingPageScenarioBuilder = scenario(ScenarioDescriptions.LANDING_PAGE.description)
                                .feed(ADMIN_PROFILES)														 
                                .exec(addCookie(Cookie(ACCESS_TOKEN, Cache.cache.get("#{email}"))																	 
                                .withDomain(server).withPath(FORWARD_SLASH)))														 
                                .exec(LandingPageScenario.renderLandingPage());

Gatling EL is working as expected.
As explained in the documentation, it doesn’t magically work in arbitrary place, only when passed to Gatling DSL methods, see Gatling - Expression Language

You must use the Session API.

Hi Stephane, thanks for replying so quickly.
I see many examples where EL is used outside of the session e.g. gatling/CoreJavaCompileTest.java at main · gatling/gatling · GitHub.

What I’m really trying to do is create a Cookie using the access token ( in the below code) retrieved after the same user logs in. This is to reduce the number of times the same user hits Auth0.
Is it possible to generate a Cookie within a session? I could then access the session token, saved after logging in.

private ScenarioBuilder loginScenarioBuilder =
  scenario(ScenarioDescriptions.LOGIN.description)
    .feed(ADMIN_PROFILES)
    .exec(loginScenario)
    .exec(session -> {
      System.out.println("Associate access token with user email in cache");
      Cache.cache.put(session.getString(EMAIL), session.getString(ACCESS_TOKEN));
      return session;
    });
         
private ScenarioBuilder landingPageScenarioBuilder =
  scenario(ScenarioDescriptions.LANDING_PAGE.description)
    .feed(ADMIN_PROFILES)
    .exec(addCookie(Cookie(ACCESS_TOKEN, "<access-token>")
      .withDomain(server).withPath(FORWARD_SLASH)))
    .exec(LandingPageScenario.renderLandingPage());

I see many examples where EL is used outside of the session e.g. gatling/CoreJavaCompileTest.java at main · gatling/gatling · GitHub.

Please read my sentence and the one in the documentation again. Gatling EL only works in Strings passed to Gatling DSL methods, which Cache.cache.get is not.

Is it possible to generate a Cookie within a session?

Yes, see Gatling - HTTP Helpers

This is to reduce the number of times the same user hits Auth0.

I guess you mean “same user” nit by the Gatling sense, but virtual users that are using the same credentials/email.
Then you need to pass a function for you cookie value where the input is the Session and the result the access token retrieved from the Cache based on the email attribute.

1 Like

Thanks Stephane for your help. The function worked a treat. Ciaran

1 Like