Dynamically selecting index from an array of responses.

282 views
Skip to first unread message

Brent

unread,
Feb 11, 2022, 2:30:19 PM2/11/22
to Gatling User Group
Hi; apologies if I've missed how to do this in the docs.

I have a json response from my first API call; and I would like to randomly select one of the objects to use for the rest of the test.

Perhaps there is a much simpler way to tackle this; but I've come up with 

Calls the first API
Save all of the response to the session
Save the length of the response's array to the session
Reads the length of the Array
This step is not compiling, but the goal would be: Reprocess the response through `jmesPath` and select an object by index.

The compile error is: ```[error]  found   : String [error]  required: io.gatling.core.check.jmespath.JmesPaths```

If this is a reasonable direction; how can I re-create `JmesPaths`?

```
      .exec(http("datasets")
        .get("/api/datasets")
        .headers(headers_0("#{api_key}"))
        .check(
          status.gte(200),
          status.lte(299),
          jmesPath("[]").saveAs("all_may_datasets"),
          jmesPath("[] | length(@)").saveAs("all_may_datasets_length")
        ))
      .pause(1)
      .exec(http("Open_dataset")
        .get(session => {
          val all_may_datasets_length = session("all_may_datasets_length").as[Int]
          println(s"***all_may_datasets_length: ${all_may_datasets_length}")
          jmesPath(s"[${all_may_datasets_length-1}]")(session("all_may_datasets").as[String])
          s"/"
        })
      )
```

Thanks

Stéphane LANDELLE

unread,
Feb 11, 2022, 3:04:23 PM2/11/22
to gat...@googlegroups.com
I'd say that you have to:
--

Stéphane Landelle

Chief Technical Officer

   

slan...@gatling.io
gatling.io
   
facebook twitter linkedin 


--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gatling/fd8a73f8-5a6e-4696-9418-325a3024b405n%40googlegroups.com.

Brent

unread,
Feb 14, 2022, 6:11:44 AM2/14/22
to Gatling User Group
Thanks for the pointers; I've tried updating the code as below


```
.exec(http("datasets")
.get("/api/datasets")
.headers(headers_0("#{api_key}"))
.check(
jmesPath("[].id").saveAs("all_datasets")

))
.pause(1)
.exec(http("Open_dataset")
.get(session => {
println(s" session.attributes: ${session.attributes}")
val random_dataset_id = session("#{all_datasets.random()}").as[Int]
```

This is erroring on:  j.u.NoSuchElementException: No attribute named '#{all_datase...

From the print line of session.attributes; I can see
 session.attributes: HashMap....., all_datasets -> [140,215, ....

So I think I'm using the wrong way to try and run Gatling Expression Language (EL) Strings against the session?


Thanks

Stéphane LANDELLE

unread,
Feb 14, 2022, 6:36:41 AM2/14/22
to gat...@googlegroups.com
Session#get takes an exact attribute key.
You can only use Gatling Expression Language when passing a String parameter to Gatling DSL methods.
You can't use Gatling Expression Language nor Gatling DSL in custom code/functions.

Where do you want to pass a random entry? In a body, right?

Then, .body(StringBody("#{all_datasets.random()"))

--

Stéphane Landelle

Chief Technical Officer

   

slan...@gatling.io
gatling.io
   
facebook twitter linkedin 

Brent

unread,
Feb 14, 2022, 7:00:56 AM2/14/22
to Gatling User Group
Got it; thank you very much for the quick responses.
Reply all
Reply to author
Forward
0 new messages