Technically speaking, session is the state of a virtual user. It is basically, a map with key strings. As, MAP[string, anystr]. Entries in a map is called Session attributes.
Session is the message that are passed along with a scenario workflow. It has to be kept in mind that, any Gatling scenario, is always backed by a Akka actor.
In the process of injecting data, we mainly inject the states inside the virtual users.
Below are the ways of doing that?
The injected data may be also retrieved with the way called fetching data. There can be two ways for fetching the data.
One is using the Gatling's Expression Language or also it can be done manually with the Session API.
Let us see how to set attributes. First, we see that the session has the following methods:
This is to keep in mind that Sessions are not mutable, because it's messages are dealt within a multi-threaded way of concurrency. And therefore the best way to deal with the state is to rely on immutability.
Below let us see, an example of a session:
val session: Session = ??? // it is a wrong usages
session.set("foo", "FOO") // wrong: the result of this set call is just discarded
session.set("bar", "BAR") // this is a proper use.
session.set("foo", "FOO").set("bar", "BAR")
We create a session instance variable named session contains a String attribute named "foo".
val session: Session = ???
Then we see what happens:
val attribute: SessionAttribute = session("foo")
In the above procedure we shall see that the (session "foo") does not return any value, but it returns a wrapper. We can access the methods to retrieve the actual value in several ways.
For example, if we take the functions as session("foo").as[Int]
This function will return a integer. And will throw a
Moreover, this function throws a
In our next example, let us take our function as session("foo").asOption[Int]:. This function returns an Option[Int]. Now this can be none if the foo attribute is or case in undefined. And which is Some(value) in all other cases, and the value is an integer, or it can be a String which can be passed as a String.
The function throws a
Let us take the example of another function session("foo").validate[Int]. This function returns an validation integer.
Subscribe to my Youtube channel for new videos : Subscribe Now