Need help - How to loop through a list and/or a map

Hej guys,

scala is pretty new for me and I have problems as soon as a leave the gatling dsl.

In my case I call an API (Mailhog) which responds with a lot of mails in json-format. I can’t grab all the values I need with “jsonPath” I need to “regex” aswell. That leads into a map and a list which I need to iterate through and save each value.

.check(jsonPath("$[*]").ofType[Map[String,Any]].findAll.saveAs("id_map")) .check(regex("href=3D\\\\\"(.*?)\\\\\"").findAll.saveAs("url_list"))

At first I wanted to loop the “checks” but I did’nt find any to repeat them without repeating the “get”-request too. So it’s a map and a list.

  1. I need every value of the map and was able to solve the problem with the following foreach loop.

.foreach("${id_map}", "idx") { exec(session => { val idMap = session("idx").as[Map[String,Any]] val ID = idMap("ID") session.set("ID", ID) }) .exec(http("Test") .get("/{ID}")) })}

  1. I need every 3rd value of the list and make a get-request on them. Before I can do this, I need to replace a part of the string. I tried to replace parts of the string while checking for them. But it won’t work with findAll.

.check(regex("href=3D\\\\\"(.*?)\\\\\"").findAll.transform(raw => raw.replace("""=\r\n""","")).saveAs("url"))

So how can I replace a part of every string in my list and how can I make a get-request on every 3rd value in the list.

Thanks in advance :slight_smile:

T

I was able to make my regex call on every element on the list with the following line of code

.check(regex("href=3D\\\\\"(.*?)\\\\\"").findAll.transform(_.map(raw => raw.replace("""=\r\n""",""))).saveAs("url"))

Now I need to make a get-Request on every 3rd element at the list. How I’m supposed to do that?

I was abole to solve the problem by myself. At first I made a little change to my check(regex …) part.

.check(regex("href=3D\\\\\"(.*?)\\\\\"").findAll.transform(_.map(raw => raw.replace("""=\r\n""",""))).saveAs("url_list"))

Then I wanted to make a Get-Request only on every third element of my list (because the URLs I extracted appeared three times per Mail).

.exec(session => { val url_list = session("url_list").as[List[Any]].grouped(3).map(_.head).toList session.set("url_list", url_list) })

At the end I iterate through my final list with a foreach-loop.

`
foreach("${url_list}", “urls”) {

exec(http(“Activate User”)
.get("${urls}")
)
}
`