web-api-testingscala-gatling

Gatling SaveAs does not save


I am practicing the Gatling API test, and I am trying to save a value in a variable and use it in another scenario step like in the code below, but it did not save at all. Can anyone help me and show me what is wrong with this code, and why saveAs is not working.

.exec(
  http("Extract Data from the response")
    .get("/users?page=2")
    .check(
      jsonPath("$.data[1].id").saveAs("userId")
    )
)
.exec(
  http("Return specific user")
    .get("/users/#{userId}")
    .check(
      jsonPath("$.id").is("7"),
      jsonPath("$.first_name").is("Michael")
    ))

userId has no value at all.

I am trying to save the value in the userId variable, and use it in another exec() block to retrieve the record of this Id.

When I print the content of the session, I can see the value is saved

 .exec {
  session => println(session)
    session // we must return something
}

Session(Check Response Body,1,HashMap(gatling.http.cache.baseUrl -> https://reqres.in/api, gatling.http.ssl.sslContexts -> io.gatling.http.util.SslContexts@3755f83c, **userId -> 8**, gatling.http.cache.dns -> io.gatling.http.resolver.ShufflingNameResolver@1d45e5a, gatling.http.cache.contentCache -> io.gatling.core.util.cache.Cache@c619d6f),OK,List(),io.gatling.core.protocol.ProtocolComponentsRegistry$$Lambda$500/0x00000008010a8978@2c11b1d,io.netty.channel.nio.NioEventLoop@226f885f)

The console error is this:

jsonPath($.id).find.is(7), but actually found nothing


Solution

  • I tried many times yesterday, and debug the code until I found the mistake in the call here:

    http("Return specific user")
        .get("/users/#{userId}")
        .check(
          **jsonPath("$.data.id").is("7"),**
          jsonPath("$.data.first_name").is("Lindsay")
    
        )
    

    The call was jsonPath("$.id").is("7") which is wrong!

    I used this code to debug code, printing the responseBody

     check(
        bodyString.saveAs("responseBody")
      ))