In this example below, I can see that path to token is correct, because when I change it I get errors such as find.exists. found nothing. Yet for some reason I can't save the token. I get Failed to build request: No attribute named 'Token' is defined
import scala.concurrent.duration._
import io.gatling.jsonpath.JsonPath
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
import io.gatling.jsonpath.AST._
class Uus extends Simulation {
val httpProtocol = http
.baseUrl("https://testsite.com")
.inferHtmlResources()
.userAgentHeader("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36")
val autentimata = Map(
"Access-Control-Request-Headers" -> "authorization",
"Access-Control-Request-Method" -> "GET",
"Origin" -> "https://testsite.com")
val autentitud = Map(
"Accept" -> "application/json, text/plain, */*",
"Origin" -> "https://testsite.com",
"authorization" -> "Bearer ${Token}")
val uri2 = "https://testsite.com"
val scn = scenario("RecordedSimulation")
.exec(http("savingtoken")
.options("/token/get?rememberMe=true")
.headers(autentimata)
.resources(http("request_2")
.get("/token/get?rememberMe=true")
// .check(jsonPath("$.data.accessToken").saveAs("Token"))
.check(status.is(200), jsonPath("$.data.accessToken").ofType[String].saveAs("Token"))
.headers(autentimata)
.basicAuth("11111111111","P2rooliall"),
http("sisselogitud")
.options("/users/11111111111")
.headers(autentimata),
http("kasutaja lehele")
.get("/users/11111111111")
.headers(autentitud)
//.check(jsonPath("$.data.accessToken").saveAs("token"))
.check(status.is(200)),
http("sündmuste lehele")
.options("/events?page=0&size=25&relation=ASSIGNEE,CREATOR&status=OPEN,REOPEN,FINISHED,ARCHIVED&sort=createdDate,desc")
.headers(autentimata),
http("sündmusteleht")
.get("/events?page=0&size=25&relation=ASSIGNEE,CREATOR&status=OPEN,REOPEN,FINISHED,ARCHIVED&sort=createdDate,desc")
.headers(autentitud)
.header("authorization", "Bearer ${Token}")
setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)
}
I think that the problem is in this line :
"authorization" -> "Bearer ${Token}"
from this block:
val autentitud = Map(
"Accept" -> "application/json, text/plain, */*",
"Origin" -> "https://testsite.com",
"authorization" -> "Bearer ${Token}")
since No attribute named 'Token' is defined
states that you are trying to use a variable not yet defined. And ,indeed, you save Token
only during scenario execution.
Gatling documentation states Expession EL :
This Expression Language only works on String values being passed to Gatling DSL methods. Such Strings are parsed only once, when the Gatling simulation is being instanciated.
So the solution would be to refactor your code and pass the block inside headers, even if it would mean a code duplication.
And you could try to verify that your token is extracted by printing its value out like this:
.exec{
session=>{
println(" Token value" + session("Token").as[String])
session
}}