Hello!
In my scenario, I have two requests. The first request returns all support languages in the body like so
["de","en","fr","it","es","nl","pl","pt","cs","sk","hu","hr"]
And I save these languages like so:
.check(
jsonPath("$[0:]")
.ofType[String]
.findAll
.saveAs("languages")
)
The second request returns JSON data, which contains, among other things
{
...
"pictureId":8,
"language":"fr",
...
},
{
...
"pictureId":4,
"language":"en",
...
},
I save this response like so:
.check(
jsonPath("$[0:]")
.ofType[String]
.findAll
.saveAs("courses")
)
I hope you get the idea.
Now, what I need respectively what I want to do, are the following steps:
I need to loop over the second JSON response, per each language, and fire up a request with the property pictureId
as a parameter.
My scenario looks like this:
...
val scn: ScenarioBuilder =
scenario("some random scenario name")
.exec(courses.httpRequest)
.exec(languages.httpRequest)
.foreach(
session => session("languages").validate[Seq[String]],
"elem",
"counter"
) {
exec(
exec { session =>
{
println("+++ elem: " + session("elem").as[String])
val tempLang: String = session("elem").as[String]
val vectorWithTempLangFilter: Vector[String] =
session("courses")
.as[Vector[String]]
.filter(
_.contains("\"language\":\"" + tempLang + "\"") == true
)
val sessionWithTempLangFilter =
session.set("sessionWithTempLangFilter", vectorWithTempLangFilter)
sessionWithTempLangFilter
}
}
exec { session =>
{
println("before foreach")
foreach("${sessionWithTempLangFilter}", "vectorElem") {
println("in foreach before rounded exec")
exec(
exec { session =>
{
println("in foreach in curly exec")
println(
">>> vectorElem: " + session("vectorElem").as[String]
)
session
}
}
)
}
println("in foreach after rounded exec")
session
}
}
)
}
What I get in the console output is similar like this:
...
+++ elem: en
before foreach
in foreach before rounded exec
in foreach after rounded exec
...
I’m really confused:
- Why is the second foreach loop executed only once, even I’m a 100% sure, that there are 6 and more elements in the JSON data
- Why are those lines in the
exec(...)
not executed? There is an element “vectorElem” in the “session”, isn’t it?
I’m working on this for a week now. Gatling+Scala is a powerful team, but everything deviating from a “straight through” scenario is really frustrating to work with.
Thank you for all your support and help!