0

How to pass or get the value from Map (random generator) to PebbleStringBody in Gatling with Scala

  val names = Iterator.continually {
    Map("name" -> s"PerfTest ${Random.alphanumeric.take(10).mkString}")
  }

.feed(names)
    .exec(http("PerfTest")
      .post("/PerfTest/bulk")
      .body(PebbleStringBody(
       """| [
          |  {% set Iteratecount = 2 %}
          | {% for t in range(1,Iteratecount) %}
          |    {
          |   "name": "{{name}}",    //cannot get the value from feeder :Map or from Json path 
          |   "TestID": "9888988FRFRFDD887887ESES",
          |   "typeId": "9888988FRFRFDD887887ESES",
          |   "statusId": "9888988FRFRFDD887887ESES",
          |   "excludedFromAutoHyperlinking": true
          |    }
          |  {% if loop.last %}
          |  {% else %},{% endif %}
          |  {% endfor %}
          |  ]""".stripMargin)).asJson
      .check(status.is(200))
    )

also could not pass the value from jsonPath to pebbleString body

.check(jsonPath("$.result.name").saveAs("name")

Please help with the solution

1 Answer 1

1

I ran your sample (against google.com) with TRACE logging enabled, using Gatling 3.8.4 and it works perfectly fine:

class PebbleSimulation extends Simulation {

  val names = Iterator.continually {
    Map("name" -> s"PerfTest ${Random.alphanumeric.take(10).mkString}")
  }

  val httpProtocol =
    http.baseUrl("https://google.com")
      .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
      .acceptLanguageHeader("en-US,en;q=0.5")
      .acceptEncodingHeader("gzip, deflate")
      .userAgentHeader(
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0"
      )

  val users = scenario("Users")
    .feed(names)
    .exec(http("PerfTest")
      .post("/PerfTest/bulk")
      .body(PebbleStringBody(
        """| [
           |  {% set Iteratecount = 2 %}
           | {% for t in range(1,Iteratecount) %}
           |    {
           |   "name": "{{name}}",    //cannot get the value from feeder :Map or from Json path
           |   "TestID": "9888988FRFRFDD887887ESES",
           |   "typeId": "9888988FRFRFDD887887ESES",
           |   "statusId": "9888988FRFRFDD887887ESES",
           |   "excludedFromAutoHyperlinking": true
           |    }
           |  {% if loop.last %}
           |  {% else %},{% endif %}
           |  {% endfor %}
           |  ]""".stripMargin)).asJson
      .check(status.is(200))
    )

  setUp(
    users.inject(atOnceUsers(1))
  ).protocols(httpProtocol)
}

HTTP request:
POST https://google.com/PerfTest/bulk
headers:
    content-type: application/json
    accept-language: en-US,en;q=0.5
    accept-encoding: gzip, deflate
    accept: application/json
    user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0
    host: google.com
    content-length: 564
body:StringRequestBody{charset=UTF-8, content= [
       {
   "name": "PerfTest mAQ6evSHPW",    //cannot get the value from feeder :Map or from Json path
   "TestID": "9888988FRFRFDD887887ESES",
   "typeId": "9888988FRFRFDD887887ESES",
   "statusId": "9888988FRFRFDD887887ESES",
   "excludedFromAutoHyperlinking": true
    }
  ,      {
   "name": "PerfTest mAQ6evSHPW",    //cannot get the value from feeder :Map or from Json path
   "TestID": "9888988FRFRFDD887887ESES",
   "typeId": "9888988FRFRFDD887887ESES",
   "statusId": "9888988FRFRFDD887887ESES",
   "excludedFromAutoHyperlinking": true
    }
        ]}

Either you're using an old buggy version of Gatling, or your issue is elsewhere.

3
  • Thanks you @Stéphane LANDELLE , I still see there is an issue here the name value should change at every iteration but i see its same. so its not dynamic when iteration is provided inside pebble string? How can I handle the for loop? Also can you provide an example with session attributes (Json Path stored value) passing to PebbleStringBody please. Will be a great help for me Oct 7, 2022 at 13:37
  • "so its not dynamic when iteration is provided inside pebble string?" No, it's not. You just get new values every time the virtual user go through the feed action. Oct 7, 2022 at 14:10
  • If you have another question, then validate this answer to this question, and create a new question. Oct 7, 2022 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.