Saulo
January 23, 2023, 5:54pm
#1
Hi people.
I’m beginner using Gatling and I have the following task to do:
Execute the first first test.
deserialize the json response.
Grab the element to use in json body in the second test in chain.
Below the code.
def DinamicoImediato() = {
val gson = new Gson()
repeat(1) {
feed(customFeeder)
.exec(http("DinamicoImediato")
.post("qrcode/dinamico/imediato/v2/")
.body(ElFileBody("bodies/pix/payloadPixDinamicoImediato.json")).asJson
.check(status.is(200))
.check(jsonPath("$..response").saveAs("response")))
.exec(session => {
val castJson = gson.fromJson(session("session").as[String].replaceAll("""\\""", ""), JsonObject().getClass)
val newVar = castJson.get("qrCodeValue").toString
http("Processamento")
.post("qrcode/processamento/")
.body(StringBody("{" +
"\n\t\"cpfCnpj\": \"09769005053\"," +
"\n\t\"qrCodeValue\": \""+newVar+"\"\n}")).asJson
.check(status.is(200))
.check(jsonPath("$..response").saveAs("novo"))
session
})
}
}
Error mensage: java.util.NoSuchElementException: No attribute named 'session' is defined
Where am I going wrong?
Hi Saulo,
Gatling is great and its cool you are trying this tool out.,
now the issue seems to me, that you are saving the response body here as a “response” session attribute here .check(jsonPath("$..response").saveAs("response")))
however you are fetching a “session” named session attribute, which probably is non-existent val castJson = gson.fromJson(session("session").as[String].replaceAll("""\\""", ""), JsonObject().getClass)
That to me seems like where the gatling is throwing the following exception
Error mensage: java.util.NoSuchElementException: No attribute named 'session' is defined
1 Like
Several things wrong.
First, you can’t use Gatling DSL inside functions, only the Session API, see Gatling - Scenario .
Then, you should capture the qrCodeValue
directly with a JsonPath (or JMESPath) expression, not try to capture a part of the response, then turn it back to String, then parse it again with GSON.
.check(jsonPath(A_PROPER_EXRESSION_TO_CAPTURE_QRCODE).saveAs("qrCodeValue")))
Finally, Scala has built in support for multilines Strings:
StringBody("""{
"cpfCnpj": "09769005053",
"qrCodeValue": "#{qrCodeValue}"
}")
1 Like
Saulo
January 23, 2023, 8:59pm
#4
Hi @kami0619 .
You are right, I wrote the code wrong. Sorry! =/
I changed in the code but i still have the same problem. Look the code below:
def DinamicoImediato() = {
val gson = new Gson()
repeat(1) {
feed(customFeeder)
.exec(http("DinamicoImediato")
.post("qrcode/dinamico/imediato/v2/")
.body(ElFileBody("bodies/pix/payloadPixDinamicoImediato.json")).asJson
.check(status.is(200))
.check(jsonPath("$..response").saveAs("body")))
.exec(session => {
val castJson = gson.fromJson(session("body").as[String].replaceAll("""\\""", ""), JsonObject().getClass)
val newVar = castJson.get("qrCodeValue").toString
http("Processamento")
.post("qrcode/processamento/")
.body(StringBody("{" +
"\n\t\"cpfCnpj\": \"09769005053\"," +
"\n\t\"qrCodeValue\":\""+newVar+"\"\n}")).asJson
.check(status.is(200))
.check(jsonPath("$..response").saveAs("novo1"))
session
})
.exec(session => {
println(session("novo1").as[String])
session
})
}
}
did you look at @slandelle suggestions too ?
Saulo
January 23, 2023, 9:21pm
#6
Hi @slandelle .
You are right, but I have question.
The response the first exec() is:
[
{
"status": 200,
"response": "{\"qrCodeValue\":\"XXXXZZZZYYYUUUOOO\",\"calendarioCriacao\":\"2023-01-23T12:44:41.383016\"}"
}
]
And I need to grab the element “qrCodeValue” and put in StringBody of second exec(), that is, I can’t put all the response. And finally, I wanted to print the response the second response (session(“novo1”).
How can I do this?
Saulo
January 24, 2023, 12:08am
#7
Hi @kami0619 .
Yes, I looked the suggestions and I made my notes. What do you think of what I pointed out?
OK, so you have to value in your JSON enveloppe that’s a JSON payload, encoded as a JSON String value.
So indeed, you have to parse in 2 phases:
jsonPath("$..response")
but then this gives you the DECODED value so you don’t have to remove the \
.
use transform
to parse this String.
val gson = new Gson()
jsonPath("$..response")
.transform { response =>
val parsedResponse = gson.fromJson(session("body").as[String]), classOf[JsonObject])
parsedResponse.get("qrCodeValue").toString
}.saveAs("qrCodeValue")
1 Like
Saulo
January 25, 2023, 12:42pm
#9
Hi @slandelle and @kami0619
Thanks for all the help. I got the solution, see the code below
def DinamicoImediato() = {
val gson = new Gson()
repeat(1) {
feed(customFeeder)
.exec(http("DinamicoImediato")
.post("url")
.body(ElFileBody("bodies/pix/payloadPixDinamicoImediato.json")).asJson
.check(status.is(200))
.check(jsonPath("$..response").transform { response =>
val parsedResponse = gson.fromJson(response.replaceAll("""\\""", ""), JsonObject().getClass)
parsedResponse.get("qrCodeValue").toString}
.saveAs("qrCodeValue")))
.exec(http("Processamento")
.post("qrcode/processamento/")
.body(StringBody("{" +
"\n\t\"cpfCnpj\": \"111111111\"," +
"\n\t\"qrCodeValue\":${qrCodeValue}\n}")).asJson
.check(status.is(200))
.check(jsonPath("$..response").transform { response =>
val parsedResponse = gson.fromJson(response.replaceAll("""\\""", ""), JsonObject().getClass)
parsedResponse.get("endToEnd").toString}
.saveAs("endToEnd")))
}