Search code examples
scalagatling

Gatling Evaluate Expression Language


Gatling parses Strings parameter values and turn them into functions that will compute a result based on the data stored into the Session when they will be evaluated.

Gatling Documentation

Is there any way to do this manually in an exec?

I have multiple request body templates which use EL attributes, the request sent will differ based on a feeder

The code I currently have is as such:

// Body itself contains attributes say ${uuid}
val body1 = Source.fromResource("body1.json")
val body2 = Source.fromResource("body2.json")

val scn: ScenarioBuilder = scenario("Dynamic body")
    .feed(feeder)
    .exec(session => {
      if(session("requestType").as[String].equals("first"))
        session.set("request", body1)
      else 
        session.set("request", body2)
      session
    }
    .exec(http("Http Call")
      .post(url)
      .body(StringBody("${request}")) 
// This won't evaluate the nested attribute and body will remain ${uuid} instead of an actual UUID
    )

I expect that there won't be a way to evaluate nested EL attributes, but is there a way to manually evaluate it using session variables? Something along the lines of

.exec(session => {
    val evaluatedBody = merge(body1, session)
    session("request", evaluatedBody)
    session
})

I've seen ELCompiler being referenced in some other questions, but not sure where to import it from and how to use it with session values


Solution

  • You should use ElFileBody that takes a file path parameter, which can be a function.

    val scn = scenario("Dynamic body")
        .feed(feeder)
        .exec(http("Http Call")
          .post(url)
          .body(
            ElFileBody(session =>
                session("requestType").as[String] match {
                  case "first" => "body1.json"
                  case _       => "body2.json"
                }
            )
          )
        )