Home > Enterprise >  How to get the return value from For loop and pass it to .body(StringBody(session => in Gatling u
How to get the return value from For loop and pass it to .body(StringBody(session => in Gatling u

Time:10-10

How to get the return value from For loop and pass it to .body(StringBody(session => in Gatling using Scala

I have created a method with for loop to generate String Array in gatling with scala

def main(args: Array[String]): Unit = {
    var AddTest: Array[String] = Array[String]()
    for (i <- 0 to 3) {
      val TestBulk: String =
        s"""{ "name": "Perftest ${Random.alphanumeric.take(6).mkString}",
            "testID": "00000000-0000-0000-0000-000000006017",
            "typeId": "00000000-0000-0000-0000-000000011001",
            "statusId": "00000000-0000-0000-0000-000000005058"};"""
      AddTest = TestBulk.split(",")
      //  val TestBulk2: Nothing = AddTest.mkString(",").replace(';', ',')
      // println(TestBulk)
    }
  }

now I want to pass the return value to .body(StringBody(session =>

    .exec(http("PerfTest Bulk Json")
      .post("/PerfTest/bulk")
      .body(StringBody(session =>
        s"""[(Value from the for loop).mkString(",").replace(';', ',')
]""".stripMargin)).asJson

Please help me with the possibilities Please let me know if

CodePudding user response:

You don't need for loop (or var or split) for this. You also do not have ; anywhere, so last replace is pointless.

    val ids = """
       "testId": "foo", 
       "typeId": "bar", 
       "statusId": "baz"
    """

    val data = (1 to 3)
     .map { _ => Random.alphanumeric.take(6).mkString }
     .map { r => s""""name": "Perftest $r"""" }
     .map { s => s"{ $s, $ids }" }
     .mkString("[", ",", "]")

   exec("foo").post("/bar").body(_ => StringBody(data)).asJson

(I added [ and ] around your generated string to make it look like valid json).

Alternatively, you probably have some library that converts maps and lists to json out-of-the box (I don't know gatling, but there must be something), a bit cleaner way to do this would be with something like this:

    val ids = Map(
       "testId" -> "foo", 
       "typeId" ->  "bar", 
       "statusId" ->  "baz"
    )

    val data = (1 to 3)
     .map { _ => Random.alphanumeric.take(6).mkString }
     .map { r => ids   ("name" -> s"Perftest $r")  }
  

   exec("foo").post("/bar").body(_ => StringBody(toJson(data))).asJson

CodePudding user response:

This Worked for me

Thanks to @dima

I build this with her suggested method.

import scala.util.Random
import math.Ordered.orderingToOrdered
import math.Ordering.Implicits.infixOrderingOps
import play.api.libs.json._
import play.api.libs.json.Writes
import play.api.libs.json.Json.JsValueWrapper

val objMap = Map("name" -> Json.toJson(s"PerfTest ${Random.alphanumeric.take(10).mkString}"),
    "testId" -> Json.toJson("00000000-0000-0000-0000"),
    "typeId" -> Json.toJson("00000000-0000-0000-0003"),
    "statusId" -> Json.toJson("00000000-0000-0000-0000"),
    "excludedFromAutoHyperlinking" -> Json.toJson(true))
  Json.toJson(objMap)

  val data2 = (1 to 3)
    .map { r => Json.toJson(objMap) }

 println(data2)```
  • Related