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

Time:10-07

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

CodePudding user response:

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.

  • Related