Looping and saving values from a json response and reusing the values in next request

Ok, but how will this escape the session (not perform the last .exec) if the json response does not contain any values with activationDate==null ?

Do not make such heavy handed use of “.exitHereIfFailed” - that exits the whole user, not just the loop. And when you use Optional, that doesn’t fail anyway.

And your syntax for if will not work. Use something more like this:

`
.doIf( session => condition-checking-code ) {
. . . // do stuff
}
.exitHereIfFailed

`

Ok,
but it is still not clear how to use your example in my concrete case with one .exec calling a service and then looping through the response and saving the elements where:

`
.check(jsonPath("$.membershipCoupons[*].coupons[?(@.activationDate==null)].offerId").findAll.optional.saveAs(“itemList”)))

`
exists, and not failing if there are no elements (just jump to the next user)
next I want check that the itemlist is not empty before I do any work (.exec activate coupon). If empty → jump to next user/session. If not empty → activate all the item in itemlist as long as it is:

`
.foreach("${itemList}", “item”)

`

What happens if you loop over an empty list?

Nothing :slight_smile:

Then I have something like this:

`

.doIf("${itemList}", “item”){
foreach("${itemList}", “item”){
exec(http(“activateCoupon”)
.post("/coupon/activate")
.header(“X-Token”, “${memberID}”)
.body(StringBody("""{“offerId”: “${item}”, “membershipId”: “${memberID}”, “osName”: “Android”, “osVersion”: “4.4”}""")).asJSON
.check(jsonPath("$.resultCode").is(“SUCCESS”)))
}
}.exitHereIfFailed

`

But I do not know how to put a condition int the .doIf("${itemList}", “item”) to make it express that it must evaluate to true and run the logic, if the item list is not empty.

Try this

`
.doIf(session => ! session(“itemList”).as[ArrayBuffer].isEmpty)

`

Don’t use ArrayBuffer which is an implementation. Use Seq which is a trait.

Then I have:

`

Ok, my complete script is failing with

`

Error:(35, 47) type Seq takes type parameters
.doIf(session => ! session(“itemList”).as[Seq].isEmpty){
^

`

Here the bugger is:

`

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

class openModelSimulation extends Simulation {

val httpConf = http
.baseURL(“https://newsite.maersk.dk”)
.acceptEncodingHeader(“gzip,deflate”)
.headers(Map(“Content-Type” → “application/json”, “charset” → “UTF-8”, “User-Agent” → “Android(4.4)/Maersk(0.4)/0.1”))

val scn = scenario(“Scenario”)

.feed(csv(“memberInfo.csv”).random)
.exec(_.set(“itemList”, Seq.empty[Int]))

.exec(
http(“getProfile”)
.get("/user/profile")
.header( “X-Token”, “${memberID}” )
.check(status.is(200))
.check(jsonPath("$.resultCode").is(“SUCCESS”))
.check(jsonPath("$.profile.memberships[0].number").is("${memberID}"))
)

.exec(http(“getCouponList”)
.get("/coupon/all")
.header(“X-Token”, “${memberID}”)
.check(status.is(200))
.check(jsonPath("$.membershipCoupons[*].coupons[?(@.activationDate==null)].offerId").findAll.optional.saveAs(“itemList”))
)

.doIf(session => ! session(“itemList”).as[Seq].isEmpty){
foreach("${itemList}", “item”){
exec(http(“activateCoupon”)
.post("/coupon/activate")
.header(“X-Token”, “${memberID}”)
.body(StringBody("""{“offerId”: “${item}”, “membershipId”: “${memberID}”, “osName”: “Android”, “osVersion”: “4.4”}""")).asJSON
.check(jsonPath("$.resultCode").is(“SUCCESS”)))
}
}.exitHereIfFailed

setUp(scn.inject(constantUsersPerSec(1) during (1 seconds))).protocols(httpConf)
}

`

Maybe someone could see what the error is aiming at?
I don’t.

Thanks!

Compiler is asking for type of data arraybuffer contains.

What Stephane said

doIf(session => ! session(“itemList”).as[Seq[String].isEmpty)

forgot to put closing square bracket in the previous post

`
doIf(session => ! session(“itemList”).as[Seq[String]].isEmpty)

`

great, it is working, this was a long thread…
thanks!!

For the given forEach loop, I’m getting this error:

13:56:54.580 [main][ERROR][ZincCompiler.scala:153] i.g.c.ZincCompiler$ - /Users/xyz/Desktop/Projects/Performance/gatling-ec2/src/test/scala/com/abc.scala:75:56: missing argument list for method foreach in trait Loops
Unapplied methods are only converted to functions when a function type is expected.
You can make this conversion explicit by writing foreach _ or foreach(_,_,_)(_) instead of foreach.
foreach("${baseComponentId}", “baseComponentId”)

has anybody experienced this too?

here’s the object that I’ve created:

object ReadComponent {
          val readComponent =
                             doIf(session => !session("baseComponentId").as[Seq[String]].isEmpty)
                                           {
                                              foreach("${baseComponentId}", "baseComponentId")
                                              exec(http("readComponent")
                                              .post("/api/graphql")
                                              .header("dataspace", "default")
                                                .body(StringBody(session => s"""query {
                                                genericReadObject(type: "BaseComponent", id: "<baseComponentId>") {
                                                  result
                                                }
                                              }"""))

                                              .body(StringBody("""{ "id": "${baseComponentId}"}""")).asJson
                                              .check(jsonPath("$.resultCode").is("SUCCESS")))
                                           }.exitHereIfFailed
      }

Your code doesn’t compile because you don’t follow the foreach syntax:

https://gatling.io/docs/current/general/scenario/#loop-statements

foreach(sequenceName, elementName, counterName) {
  myChain 

}