Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access session values while executing ChainBuilder

Tags:

scala

gatling

Is it possible to access session values and still execute a ChainBuilder object? The way my code’s setup at the moment, the http request never actually gets executed since it's just returning the session. I need the session attributes to build a header parameter with all those session values.

val testTCSService = scenario("Some Scenario")
    .doIf(session => session.contains("value")) {
      exec(session => if(session.contains("otherValue")) session else session.markAsFailed)
        .exitHereIfFailed
        .exec{session => OtherClass.verifyHTTPCall(session("value").as[String], session("secondValue").as[String], session("thirdValue").as[String])
        session}
    }

def verifyHTTPCall(token: String, realmId: String, userId: String): ChainBuilder = {
        exec(http("HTTP Call")
          .post("Some URL")
          .header("header_value", generateHeader(value, secondValue, thirdValue))
          .check(status.is(200))
    }
like image 483
user10349721 Avatar asked Jan 27 '23 11:01

user10349721


1 Answers

It won't work that way, because Gatling is not working as you (and basically everybody who starts using it) think. Scenario Chain builder is executed only once per simulation and it creates chain of actions which is then used by each individual user as something like template for all requests. So what you need to do is not to create ChainBuilder in session but to extract data from session in ChainBuilder. In your case it will be easier to use Session.Expression[T] (some actions take this type of param which is a function of type Session => T so your code could look like:

val testTCSService = scenario("Some Scenario")
  .doIf(session => session.contains("value")) {
    exec(session => if(session.contains("otherValue")) session else session.markAsFailed)
      .exitHereIfFailed
      .exec(verifyHTTPCall)
  }

def generateHeader(a: String, b: String, c: String): String = ???

def verifyHTTPCall = http("HTTP Call")
  .post("Some URL")
  .header("header_value", session => generateHeader(session("value").as[String], session("secondValue").as[String], session("thirdValue").as[String]))
  .check(status.is(200))

Or simpler, with passing whole session instead of 3 attribute values, and getting rid of redundant doIf (which skips whole scenario if no value is set, so whole fail session mechanism would not work):

def generateHeader(session: Session): String = {
  //Extract values from session and build header
}

val testTCSService = scenario("Some Scenario")
  .exec(session => if(session.contains("otherValue")) session else session.markAsFailed)
    .exitHereIfFailed
    .exec(http("HTTP Call")
      .post("Some URL")
      .header("header_value", session => generateHeader(session))
      .check(status.is(200)))
like image 174
Mateusz Gruszczynski Avatar answered Feb 03 '23 23:02

Mateusz Gruszczynski