How to access a value stored in a session from within a feeder in Gatling - scala

In my Gatling scenario, a value is stored in the session for a user.
Later in the same scenario, feed is called and passed a custom feeder. The custom feeder needs to generate its next value using the stored value in the session.
val MyScenario = scenario("ScenerioName")
.repeat(10, "repetition") {
exitBlockOnFail {
group("WorkflowGroupName") {
exec(session => {
// SETTING A VALUE INTO THE USER'S SESSION
session.set("sessionVariable", 99) // value that is stored changes for every run of the workflow (99 just for example purposes)
})
// CUSTOM FEEDER THAT GENERATES ITS NEXT VALUE USING THE SESSION VARIABLE 'sessionVariable' STORED ABOVE
.feed(myFeeder)
.group("RequestGroup1") {
exec(httpPost1)
}
}
}
}
val myFeeder = Iterator.continually(Map("jsonFileValue" -> {
// WANT TO RETRIEVE VALUE OF 'sessionVariable' STORED IN THE SESSION
val returnValue = /* logic that generates its value based on value of 'sessionVariable' retrieved */
returnValue
}
))
val httpPost1 = http("Request1")
.post("http://IPAddress/service.svc")
.headers(httpHeaders)
.body(ELFileBody("MyJsonFile.json"))
.check(status.is(200))
val httpHeaders = Map(
"Content-Type" -> "application/json; charset=UTF-8",
"X-Pod" -> ""
)
How can I pass this stored session value to the feeder or have the feeder retrieve this value from the session?

As the documentation states:
Sometimes, you could want to filter the injected data depending on
some information from the Session.
Feeder can’t achieve this as it’s just an Iterator, so it’s unaware of
the context.
If your values are independent of the tests you run, maybe a good way can be to generate a csv before you run your tests and then feed this csv to your tests.

Related

Gatling session - get attribute as Long

I am a new in Scala and got some problems with casting from String to Long. I try to get Gatling session value as Long in request. Before in exec() part, I try to set the userId value
def setUserId(): ChainBuilder = {
exec(session => session
.set("userId", Random.nextLong())
)
}
Next, in request creator I want to use it like that because I need a new userId every call:
object UserRequestCreator {
def sampleUserRequest(currency: String): Request = {
Data data = new Data()
data.setUserId("${userId}".toLong)
data.setCurrency(currency)
}
}
Test scenario:
exec(setUserId())
.exec(http("postUser")
.post(endpointUser).asXml
.headers(headers)
.body(StringBody(toXmlString(sampleUserRequest("EUR"), classOf[Request])))
.check(status.is(200))
but receive error:
java.lang.NumberFormatException: For input string: "${userId}"
How to fix that in Scala?
I also try Long.valueOf, JLong.parseLong("${userId"}, 16), Try(BigDecimal(...)) and more but nothing can help. I think the problem is with $ symbol, but I don't see any different way to get this value from the session. Maybe it is possible to store Long in the Gating session?
From the documentation and based on your current code, one way to do it is like that:
// with a function payload
http("name").post("/")
.body(StringBody(session => s"""{ "foo": "${session("dynamicValueKey").as[String]}" }"""))
Thus, in your case:
StringBody(session => toXmlString(sampleUserRequest(session)("EUR"), classOf[Request])
def sampleUserRequest(session: Session)(currency: String): Request = {
//...
data.setUserId(session("userId").as[Long])
}

How to Save a Response Body and Use It Throughout the Gatling Execution

I am using 2 API calls in my Gatling simulation. One API returns the authentication token for the second, so I need to call the token generation API call only once during the execution and use it's generated token for the second API throughout the execution. But this works only for the first cycle of execution and the token that I have saved is not getting used for the remaining executions.
object KeycloakToken extends CMS {
def request(conf: ExecutionConfig): HttpRequestBuilder = {
http("Get Auth token from Keycloak")
.post(s"${conf.authUrl}/token")
.body(StringBody(""" {
"realm": "realm",
"clientId": "clientId",
"clientSecret": "clientSecret",
"grantType": "urn:ietf:params:oauth:grant-type:token-exchange",
"userName" : "userName"
} """)).asJson
.check(jsonPath("$.access_token").saveAs("token"))
}
}
object getOffers extends CMS {
def request(conf: ExecutionConfig): HttpRequestBuilder = {
http("getOffers")
.post(s"$context")
.body(StringBody(""" Body """)).asJson
.headers(headers)
.header("Authorization", s => s"Bearer ${s.attributes("token")}")
.check(jsonPath("$.data.offers.offers[0].id").exists)
}
}
execute.apply(
scenario("service")
.exec(session => session.set("counter" , myGlobalVar.getAndIncrement))
.doIf(session => session("counter").validate[Int].map(i => i == 0)) {
exec(KeycloakToken.request(conf)) //--call only once
}
.exec(getOffers.request(conf))
)
A gatling session object is unique to each user. So when you set your counter session variable as the first step of your scenario then use a doIf to only get the token if counter==0 only the first user to execute will ever try to get the token.
Since the session is unique to that user, none of the other users will have a value for token in their session object.
What you're trying to do seems to be a pretty common issue - make a single request to get some kind of data, then have that data shared among all the users. eg: here
Note that it looks like this kind of scenario will be easier once gatling 3.4

How to set different protocol for each user (threads running) in gatling

I am trying to create a performance test suite where i have array of ID's that will be picked in random. For each ID , there is a designated auth_token assigned .
In the protocol , if i pass the method where the random ID's would take , it always sets it to that particular ID for the entire operation .
I am expecting something like , i am defining 10 virtual users and for each user the protocol should change the ID and continue the scenario execution .
Currently Gatling is setting the protocol at the first and uses the same protocol for all the 10 users.
id = random.generate //generate random id
authHeader = Method(id);
def method (id:String) : String{
if(id=="id1")
return token1
else if(id=="id2")
return token2
""
}
val httpProtocol = http.baseUrl(baseURI)
.acceptHeader(header)
.authorizationHeader(authHeader)
.contentTypeHeader(contentType)
.userAgentHeader(agentHeader)
val scn1: ScenarioBuilder = scenario("name")
.exec(http("scenario1")
.post(device_context)
.body(payload)
.check(status.is(202)))
setUp(scn1.inject(atOnceUsers(2)).protocols(httpProtocol))```
In the above code i need the suite to run for 2 different id.
To make this work, you're going to have to use a session variable to hold your authHeader. The DSL methods define builders that are only executed once - which explains what you're seeing.
The best way to do this would be to construct a feeder to hold your ids and auth tokens (I'm assuming you're not using the id anywhere else)
So define a feeder that maps a key (just a string) to a map that contains the id and auth token
//a list of strings to hold the auth tokens
private val ids = List(Map("id" -> "id1", "auth" -> "token1"), Map("id" -> "id2", "auth" -> "token2"), ...)
//the feeder to put a random token into the session
private val idFeeder = Iterator.continually(Map("id" -> Random.shuffle(ids).head))
now when you call .feed on idFeeder you get a random Map that has keys for "id" and "auth" in the session variable "id"
so update your scenario to use the feeder and set an Authorization header (and remove .authorizationHeader from your protocol definition)
val scn1: ScenarioBuilder = scenario("name")
.feed(idFeeder)
.exec(http("scenario1")
.header("Authorization", "${id.auth})"
.post(device_context)
.body(payload)
.check(status.is(202)))
in your body you can access the user id string with "${id.id}"
alternatively you could update your protocol definition to have the ${id} reference, but I find it nicer to have the feeder value used in the same block as the call to .feed

Gatling pass feeder as a parameter to object

I have a Gatling script with few objects that I use for my tests. One of the objects is used to authenticate users and users are passed to test using a feeder. The feeder is now defined on top in the beginning of the simulation class like this:
val feeder = csv("users.csv").circular
The feeder provides pairs of usernames and passwords. The object looks like:
object Auth {
val login = {
feed(feeder)
.exec(http("get_auth_token")
.post("/get_auth_token")
.body(StringBody("""{
"username": "${username}",
"password": "${password}"
}""")).asJSON
// this check extracts accessToken from JSON received in response
.check(
status.find.in(200,304,201,202,203,204,205,206,207,208,209),
jsonPath("$.response_body.access.token").saveAs("accessToken"),
jsonPath("$.response_body.refresh.token").saveAs("refreshToken")
)
).exitHereIfFailed
}
val refreshAuthToken = {
exec(http("refresh_auth_token")
.post("/refresh_auth_token")
.header("Authorization", "Bearer ${refreshToken}")
.check(
status.find.in(200,304,201,202,203,204,205,206,207,208,209),
jsonPath("$.response_body.access.token").saveAs("accessToken")
)
).exitHereIfFailed
}
}
Then I have a scenario that looks like:
val myScenario = scenario("My test")
.exec(Auth.login)
// Do some stuff here
.exec(Auth.refreshAuthToken)
// Do more stuff here
Then I have a setup that runs this single scenario.
What I would like to have is additional scenario that will run alongside with the get additional scenario that will run alongside with myScenario. This scenario will also perform Auth.login, but I want it to use a different set of users - so I need to have two separate feeders that will be passed to Auth.login from scenario. Can someone suggest how this can be achieved?

Gatling 2 dynamic queryParam on each request

I am trying to run a load test using Gatling 2. I need to generate one of the query parameters dynamically on each request.
My scenario is defined like this:
val scn = scenario("Load Test Scenario")
.exec(
http("Test API")
.post(url)
.body(StringBody("Some XML"))
.queryParam("x", DigestUtils.md5Hex(generateX().getBytes("UTF-8")))
)
def generateX() : String = {
// generate random string and return
}
This only calls generateX once and uses the result in each request. Is there anyway to have the generateX call on every request?
You have to pass a function, not a value. See Gatling documentation about Expression.
Here, you can just discard the session input parameter as you don't use it, so you can simply write:
.queryParam("x", _ => DigestUtils.md5Hex(generateX().getBytes("UTF-8")))