Gatling issue

Hi Guys

I got following Jason body in my request. I have captured value for event_token in eventToken parameter and substituting this as per below in Jason body

.body(StringBody(session =>

s"""

{

| “event”: {

| “id”: “CREATE”,

| “summary”: “4th Page Check your answers - Event summary 9999999”,

| “description”: "4th Page Check your answers - Event description 9999999"

| },“event_token”: “”"" + “${eventToken}” + “”"",

| “ignore_warning”: false

|}

""".stripMargin)).asJSON

for some reason it’s not substitution value so my request is keeps failing so I guess I am not doing in correct way.

See below the log from gatling

stringData=

{

| “event”: {

| “id”: “CREATE”,

| “summary”: “4th Page Check your answers - Event summary 9999999”,

| “description”: "4th Page Check your answers - Event description 9999999"

| },“event_token”: “${eventToken}”,

"ignore_warning": false

}

Please suggest how to fix this issue

Looks event token is not parameterized properly or extracted properly. Check with it.

Try to print Session parameters before execution to see if effectively the eventToken param is present.

El dijous, 28 juny de 2018 15:56:00 UTC+2, Kapil va escriure:

I’ve just pushed a better explanation on how Gatling EL works: https://github.com/gatling/gatling/commit/2b453e0fd21c6420357b194024b22c2180ccb049

I have printed session parameter value and it’s printing correctly as follows

eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI1ZGlkbmUwM2I0dmw2YW04bjltaDkzcXBraSIsInN1YiI6IjE3NjQ3NSIsImlhdCI6MTUzMDQ2MjQ1NCwiZXZlbnQtaWQiOiJDUkVBVEUiLCJjYXNlLXR5cGUtaWQiOiJBQVQiLCJqdXJpc2RpY3Rpb24taWQiOiJBVVRPVEVTVDEiLCJjYXNlLXZlcnNpb24iOiJiZjIxYTllOGZiYzVhMzg0NmZiMDViNGZhMDg1OWUwOTE3YjIyMDJmIn0.y6aOh0gyvNr-2266aLv73Kpchez94kHLfgo9VkiFDhw

so I am trying and substituting this in son body as follows but it's failing and don't really understand what's wrong in this syntax for substituting parameter value. Please suggest ?


**.body(StringBody(session =>**

**  s"""**

**     {**

**     | "event": {**

**     |    "id": “CREATE”,**

**     |    "summary": "4th Page Check your answers - Event summary 9999999",**

**     |    "description": "4th Page Check your answers - Event description 9999999"**

**     |  },"event_token": """"  + "${eventToken}" +   """",**

**     |  "ignore_warning": false**

**     |}**

**     """.stripMargin)).asJSON**

**however if I am trying this hardcoded value then it's working OK because this token is valid for 1 hour atleast.**

"event_token":  "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI1ZGlkbmUwM2I0dmw2YW04bjltaDkzcXBraSIsInN1YiI6IjE3NjQ3NSIsImlhdCI6MTUzMDQ2MjQ1NCwiZXZlbnQtaWQiOiJDUkVBVEUiLCJjYXNlLXR5cGUtaWQiOiJBQVQiLCJqdXJpc2RpY3Rpb24taWQiOiJBVVRPVEVTVDEiLCJjYXNlLXZlcnNpb24iOiJiZjIxYTllOGZiYzVhMzg0NmZiMDViNGZhMDg1OWUwOTE3YjIyMDJmIn0.y6aOh0gyvNr-2266aLv73Kpchez94kHLfgo9VkiFDhw",

I think I’ve spotted where the mistake is… try to not use an Expression function but a Gatling EL String.

However, if you want to use it this way, you need to get explicitly the value from session (no EL involved) like:


**.body(StringBody(session =>**

**  s"""**

**     {**

**     | "event": {**

**     |    "id": “CREATE”,**

**     |    "summary": "4th Page Check your answers - Event summary 9999999",**

**     |    "description": "4th Page Check your answers - Event description 9999999"**

**     |  },"event_token": """"  + session("eventToken").as[String]  +  """",**

**     |  "ignore_warning": false**

**     |}**

**     """.stripMargin)).asJSON**

Not sure if this compiles, but you get the general idea :)

El diumenge, 1 juliol de 2018 19:53:46 UTC+2, Kapil va escriure:

Thanks I tried .body(StringBody(s""" instead of .body(StringBody(s""" and it worked,

I am now trying to replace 999999 with randomly generated number for each iteration but every time it’s picking same random number any idea why ?

"summary": "4th Page Check your answers - Event summary ${scala.util.Random.nextInt(999999999)}",

Please, post the entire .body definition

El dilluns, 2 juliol de 2018 9:42:17 UTC+2, Kapil va escriure:

Here you go

.body(StringBody(s"""{"event": {"id": “CREATE”,****“summary”: "4th Page Check your answers - Event summary ${scala.util.Random.nextInt(999999999)}",****“description”: "4th Page Check your answers - Event description ${scala.util.Random.nextInt(999999999)}},“event_token”: “”"" + “${eventToken}” + “”"","ignore_warning”: false****}""")).asJSON

instead of generating unique random value for each iteration it’s using same value i.e., 4th Page Check your answers - Event summary 345234654

What I really don’t understand is why you rely on Scala’s String interpolation, instead of storing the variables you need in Gatling’s Session. I’ve coded an example (maybe it doesn’t work, but you can see what I mean).

I find easier to store the variables in a previous step (you can always remove them in next steps).

class KapilSimulation extends Simulation {

  private val rng: Random = new Random()

  private def createToken(): String = rng.alphanumeric.take(32).mkString
  private def createRandomInt(): Int = rng.nextInt(999999999)

  setUp(
    scenario("My Scenario")
      .exec(
        _.setAll(
          ("eventToken", createToken()),
          ("eventRandomInt", createRandomInt())
        )
      ).exec(
        http("My HTTP request")
          .post("/random")
          .body(StringBody("""{"token": "${eventToken}", "number": ${eventRandomInt}}""")).asJSON
          .check(status is 200)
      ).inject(atOnceUsers(5))
  ).protocols(
    http.baseURL(gatewayUrl)
      .acceptHeader("application/json")
      .contentTypeHeader("application/json")
      .acceptEncodingHeader("gzip, deflate")
      .userAgentHeader("Gatling")
      .warmUp(s"$gatewayUrl/health")
      .shareConnections
  ).maxDuration(duration)

}