How to share session between scenarios which were set up using 'andThen'

Hi there,

Problem I’m trying to solve: I have different endpoints that I have to access during a test. For example:

  • create an order(1st endpoint)
  • change some settings using orderId from prev step(2-nd endpoint)
  • get info about order using orderID (3-rd endpoint)

I was told here that it can not be done within single scenario(as I need multiple protocols, one per endpoint) and that I should split into multiple scenarios and use .andThen to run them sequentially.

Only problem which is left is how to pass data from one scenario to another and do this per user?

thank you.

session = virtual user

You can’t share virtual user among scenarios.

Now, I don’t understand what you’re struggling with exactly. Why do you thing you need multiple scenarios and not just one single scenario where virtual users would hit endpoint1 then endpoint2 then endpoint3?

Have you gone though the official tutorials and/or the online trainings?

all endpoints has different base urls and headers. I was trying to solve this using protocols, is there an other way around?

I was asking here if I can attach multiple protocols to scenario and choose which one is used during an execution and you told me that it is not doable and I should go for multiple scenarios + andThen.

I’ve gone trough all docs, tuts and stackoverflow so far.

thanks.

meanwhile I’m going to try to share data between scenarios through variable. Push\pull through list of something.

all endpoints has different base urls and headers.

You don’t have to use a protocol for this. You can perfectly define requests with absolute urls and their own headers.

I was asking here if I can attach multiple protocols to scenario and choose which one is used during an execution and you told me that it is not doable and I should go for multiple scenarios + andThen.

Well, if you ask the wrong question, you get the wrong answer :slight_smile:
That’s why it’s very important to explain in details what you’re really trying to achieve, not the solution you have in mind.

In my case I would say I have to use protocols for this. Otherwise project code with hundreds of tests and dozen or protocols will turn into a mess.

There was nothing wrong either in my question or in your answer, it is working perfectly fine, thank you.

For those who might want to follow same path as I did, I’m using following code to share session between scenario:

var sharedData = scala.collection.mutable.Map[String, scala.collection.mutable.Queue[io.gatling.core.session.Session]]()

def shareSession(scenarioName: String, session: io.gatling.core.session.Session): Unit = {
    if (!sharedData.contains(scenarioName)) {
        sharedData(scenarioName) = scala.collection.mutable.Queue[io.gatling.core.session.Session]()
    }
    sharedData(scenarioName).enqueue(session)
}

def getSharedSession(scenarioName: String): io.gatling.core.session.Session = {
    val session = sharedData(scenarioName).dequeue()
    println("Get shared session: " + scenarioName)
    println(session)
    session
}

Then to get values you need:
.exec(session => {
val sharedSession = getSharedSession(scenarioName)
session.setAll(Map(
“some_key” → sharedSession(“some_key”).as[String],
))
})

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.