简体   繁体   中英

Gatling & Scala : How to split values in loop?

I want to split some values in loop. I used split method in check and it works for me. But, there are more than 25 values of two different types. So, I am implementing loop in scala and struggling. Consider the following scenario:

import scala.concurrent.duration._
import io.gatling.core.Predef._
import io.gatling.http.Predef._

class testSimulation extends Simulation {

    val httpProtocol = http
    .baseURL("https://website.com")
    .doNotTrackHeader("1")
    .disableCaching

val uri1 = "https://website.com"

val scn = scenario("EditAttribute")
    .exec(http("LogIn")
        .post(uri1 + "/web/guest/")
        .headers(headers_0)
    .exec(http("getPopupData")
        .post("/website/getPopupData")
        .check(jsonPath("$.data[0].pid").transform(_.split('#').toSeq).saveAs("pID")))  // Saving splited value
    .exec(http("Listing")
        .post("/website/listing")
        .check(jsonPath("$.data[*].AdId").findAll.saveAs("aID"))                        // All values are collected in vector 
//      .check(jsonPath("$.data[*].AdId").transform(_.split('#').toSeq).saveAs("aID"))  // Split method Not working for batch
//      .check(jsonPath("$.data[*].AdId").findAll.saveAs("aID"))                        // To verify the length of array (vector)
        .check(jsonPath("$.data[0].RcId").findAll.saveAs("rID")))
    .exec(http("UpdatedDataListing")
        .post("/website/search")
        .formParam("entityTypeId", "${pId(0)}")                                 // passing splited value,  perfectly done
        .formParam("action_id", "${aId(0)},${aId(1)},${aId(2)},..and so on)      // need to pass splitted values which is not happening
        .formParam("userId", "${rID}")
// To verify values on console (What value I m getting after splitting)...
    .exec(  session =>  {
            val abc = session("pID").as[Seq[String]]
            val xyz = session("aID").as[Seq[String]]
            println("Separated pId ===>  "   +abc(0))               // output - first splitted value
            println("Separated pId ===>  "   +abc(1))               // split separater
            println("Separated pId ===>  "   +abc(2))               // second splitted value
            println("Length  ===>   "   +abc.length)                // output - 3
            println("Length  ===>   "   +xyz.length)                // output - 25
            session
                        }
         )
    .exec(http("logOut")
        .get("https://" + uri1 + "/logout")
        .headers(headers_0))

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

I want to implement a loop which performs splitting of all (25) values in session. I do not want to do hard coding. I am newbie to scala and Gatling as well.

Since it is a session function the below snippet will give a direction to continue ,use split just like you do in Java :-

  exec { session =>
    var requestIdValue = new scala.util.Random().nextInt(Integer.MAX_VALUE).toString();
    var length = jobsQue.length
    try {
      var reportElement = jobsQue.pop()
      jobData = reportElement.getData;
      xml = Configuration.XML.replaceAll("requestIdValue", requestIdValue);

      println(s"For Request Id : $requestIdValue  .Data Value from feeder is : $jobData Current size of jobsQue : $length");
    } catch {
      case e: NoSuchElementException => print("Erorr")
    }

    session.setAll(
      "xmlRequest" -> xml)
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM