# Using .andThen in the simulation

**URL:** <https://community.gatling.io/t/using-andthen-in-the-simulation/6111>\
**Category:** Gatling (Open-Source)\
**Created:** [May 22, 2021, 3:40pm UTC](https://community.gatling.io/t/using-andthen-in-the-simulation/6111 "2021-05-22T15:40:11Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![gkandale](https://avatars.discourse-cdn.com/v4/letter/g/c2a13f/32.png) [@gkandale](https://community.gatling.io/u/gkandale)\
**Post date:** [May 22, 2021, 3:40pm UTC](https://community.gatling.io/t/using-andthen-in-the-simulation/6111/1 "2021-05-22T15:40:11Z")

</div>

Hi All,

I am trying to include scripts into my suite of tests that will use .andThen with a parent and child script. The parent script creates all the dependancies such as users and tenants and the child will create the assets which will belong to the tenants and users. My framework has the code, scenarios and simulations separated out. I am not sure how to integrate andThen into my scripts.

**Scenario.scala :**  
package msx.performance.com.api.manageddevice.scenario

import io.gatling.core.Predef.\_  
import io.gatling.core.structure.{ChainBuilder, ScenarioBuilder}  
import msx.performance.com.api.consume.request.MSXPlatformConsume  
import msx.performance.com.config.\_  
import msx.performance.com.api.manageddevice.request.\_  
import msx.performance.com.api.idm.request.\_

object Scenario extends BaseSimulation {

def createScenario(name: String, chains: ChainBuilder\*): ScenarioBuilder = {  
scenario(name)  
.exec(chains)  
.pause(Constants.pause)  
.exec { session =\>  
println(session(“deviceId”).as[String])  
session  
}  
}

val scnCreateNewTenantSite: ScenarioBuilder = createScenario(  
“SCN\_MSX\_CREATE\_TENANT\_SITE”,  
MSXPlatformLogin.getTokenFromAuthorize,  
MSXPlatformLogin.getIsTokenValid,  
MSXPlatformTenant.createNewTenant  
)

val scnCreateDeviceSite: ScenarioBuilder = createScenario(  
“SCN\_MSX\_CREATE\_DEVICE\_SITE”,  
MSXPlatformLogin.getTokenFromAuthorize,  
MSXPlatformLogin.getIsTokenValid,  
MSXPlatformManagedDeviceSites.findSite,  
MSXPlatformManagedDeviceSites.addDevice,  
MSXPlatformManagedDeviceSites.addDeviceToSite  
)  
}

**Simulation.scala :**

package msx.performance.com.api.manageddevice.simulation

import io.gatling.http.Predef.\_  
import io.gatling.core.Predef.{nothingFor, _}  
import msx.performance.com.api.manageddevice.scenario.Scenario  
import msx.performance.com.config._

import scala.concurrent.duration.DurationInt

class Simulation extends BaseSimulation {

val httpScenarios = Map (  
“setUpTestEnvironment” → List(  
Scenario.scnCreateNewTenantSite.inject(atOnceUsers(1))  
),  
“createDevicesAttachtoSite” → List(  
Scenario.scnCreateDeviceSite.inject(nothingFor(5 seconds), rampUsers(Constants.userCount) during(Constants.rampDuration seconds))  
)  
)

setUp(httpScenarios(Constants.testScenarios):\_\*)  
.protocols(httpConf)  
.maxDuration(Constants.testDuration seconds)  
.assertions(global.responseTime.percentile4.lte(Constants.responseTimeLTE),  
global.successfulRequests.percent.gte(Constants.successRateGTE))

}

The above works great with concurrent scripts running, but I need to write it in a way where setUpTestEnvironment runs once and then createDevicesAttachtoSite runs several times after first script finishes. Any help will be great!!

-GK

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [May 23, 2021, 7:25pm UTC](https://community.gatling.io/t/using-andthen-in-the-simulation/6111/2 "2021-05-23T19:25:37Z")

</div>

I guess you visited [Sequential Scenarios](https://gatling.io/docs/current/general/simulation_setup/#sequential-scenarios) in our documentation?

I’m sorry, I cannot manage to read your code.  
I stopped when I saw that:

```auto
object Scenario extends BaseSimulation

```

A Scenario IS NOT a Simulation.  
To shorten our documentation, a Simulation often declares its own scenario, but never we mixed the concept of Scenario and Simulation (if that is really the case, please point it to us, it is a terrible mistake).

I often use the allegory with the theater play.

> Gatling term | Theater term | Comment |
> 
> - | - | - |  
> Virtual User | Actor/Actress | A virtual user will simulate what a real user will do in such situations |  
> Scenario | Script | What an actor/actress will say, where (s)he will be placed, etc. |  
> Simulation | A play | When each actor will enter on stage, how many actors, etc. |

In this allegory, I assume you want to have (at least) 2 scenes.  
[`andThen`](https://gatling.io/docs/current/general/simulation_setup/#sequential-scenarios) will be a perfect use, here.

But `andThen` is a keyword to use in the `setUp` of a Simulation to coordinates the different scenarios in play.

I hope that helps you!

Cheers!

---

<div class="post-metadata">

**Author:** ![gkandale](https://avatars.discourse-cdn.com/v4/letter/g/c2a13f/32.png) [@gkandale](https://community.gatling.io/u/gkandale)\
**Post date:** [May 24, 2021, 12:55pm UTC](https://community.gatling.io/t/using-andthen-in-the-simulation/6111/3 "2021-05-24T12:55:28Z")

</div>

Hi, No need to talk down to me, I reached out for help man. Honestly, I Don’t know what you are talking about. First of all I obviously omitted the api scripts and my base class (baseSimulation) - where I have my login, and all my repeatable code which extends gatling’s simulation library. Thanks for telling me how performance automation works ☹

I want to take my scenario.scala (the order and step by step instructions of my test scripts which I’ve separated into a test environment setup scenario and an asset creation scenario) and run them in an “andThen” Simulation (test setup first and then asset creation second). I am only asking about the syntax based on how I have used chain builder and scenario builder. If you don’t want to help just say you don’t want to help.

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [May 24, 2021, 1:34pm UTC](https://community.gatling.io/t/using-andthen-in-the-simulation/6111/4 "2021-05-24T13:34:47Z")

</div>

Personally, I don’t understand why you feel “talked down to”. Maybe not being a native english speaker.

First, Sébastien pointed out that you’re misusing the term “Scenario”. In Gatling’s terminology, a Scenario is not a Simulation so “object Scenario extends BaseSimulation” is wrong. At best, it makes things confusing for people using the proper terminology. Worse, it can cause some class name clashes with [https://github.com/gatling/gatling/blob/main/gatling-core/src/main/scala/io/gatling/core/scenario/Scenario.scala](https://github.com/gatling/gatling/blob/main/gatling-core/src/main/scala/io/gatling/core/scenario/Scenario.scala).

Then, Sébastien provided you with a pointer to the “andThen” documentation: [https://gatling.io/docs/current/general/simulation\_setup/#sequential-scenarios](https://gatling.io/docs/current/general/simulation_setup/#sequential-scenarios)  
Could you please elaborate on what you don’t understand?

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [May 24, 2021, 1:37pm UTC](https://community.gatling.io/t/using-andthen-in-the-simulation/6111/5 "2021-05-24T13:37:40Z")

</div>

Gatling’s terminology: [https://gatling.io/docs/current/general/concepts/](https://gatling.io/docs/current/general/concepts/)

---

<div class="post-metadata">

**Author:** ![gkandale](https://avatars.discourse-cdn.com/v4/letter/g/c2a13f/32.png) [@gkandale](https://community.gatling.io/u/gkandale)\
**Post date:** [May 24, 2021, 2:31pm UTC](https://community.gatling.io/t/using-andthen-in-the-simulation/6111/6 "2021-05-24T14:31:58Z")

</div>

OK, sorry, I named my base class in an ugly way, I will fix so that SB can read my code without giving him a fluttering heart beat…I know what scenario and simulation mean…That was not anywhere close to my question. I need to figure out the syntax for .andThen. I used your documentation just now but I get a type mismatch error. If you can help with that it would be more helpful too me…Again if you don’t want to help with “andThen” just say so. I’m sorry to seem so ungrateful but for some reason you are focusing in on something that is neither here nor there and has been working in my scripts for 2+ years now.

```auto
setUp(
  Scenario.scnCreateNewTenantSite.inject(atOnceUsers(1))
    .andThen( Scenario.scnCreateDeviceSite.inject(rampUsers(Constants.userCount).during(Constants.rampDuration seconds)))
 ).assertions(global.responseTime.percentile4.lte(Constants.responseTimeLTE),
    global.successfulRequests.percent.gte(Constants.successRateGTE))

```

10:22:08.465 [main][ERROR][ZincCompiler.scala:153] i.g.c.ZincCompiler$ - /Users/gkandale/gatling/msx4.0/us33569-manageddevice-scale-scripts/msx-performance-tools/src/test/scala/msx/performance/com/api/manageddevice/simulation/Simulation.scala:38:54: type mismatch;  
found : io.gatling.core.structure.PopulationBuilder  
required: io.gatling.commons.validation.Validation[io.gatling.core.structure.PopulationBuilder] =\> ?  
.andThen( Scenario.scnCreateDeviceSite.inject(rampUsers(Constants.userCount).during(Constants.rampDuration seconds)))  
^  
10:22:08.501 [main][ERROR][ZincCompiler.scala:124] i.g.c.ZincCompiler$ - one error found  
10:22:08.505 [main][ERROR][ZincCompiler.scala:220] i.g.c.ZincCompiler$ - Compilation crashed  
sbt.internal.inc.CompileFailed: null  
at sbt.internal.inc.AnalyzingCompiler.call(AnalyzingCompiler.scala:253)  
at sbt.internal.inc.AnalyzingCompiler.compile(AnalyzingCompiler.scala:122)  
at sbt.internal.inc.AnalyzingCompiler.compile(AnalyzingCompiler.scala:95)  
at sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$4(MixedAnalyzingCompiler.scala:91)  
at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)  
at sbt.internal.inc.MixedAnalyzingCompiler.timed(MixedAnalyzingCompiler.scala:186)  
at sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$3(MixedAnalyzingCompiler.scala:82)  
at sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$3$adapted(MixedAnalyzingCompiler.scala:77)  
at sbt.internal.inc.JarUtils$.withPreviousJar(JarUtils.scala:215)  
at sbt.internal.inc.MixedAnalyzingCompiler.compileScala$1(MixedAnalyzingCompiler.scala:77)  
at sbt.internal.inc.MixedAnalyzingCompiler.compile(MixedAnalyzingCompiler.scala:146)  
at sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileInternal$1(IncrementalCompilerImpl.scala:343)  
at sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileInternal$1$adapted(IncrementalCompilerImpl.scala:343)  
at sbt.internal.inc.Incremental$.doCompile(Incremental.scala:120)  
at sbt.internal.inc.Incremental$.$anonfun$compile$4(Incremental.scala:100)  
at sbt.internal.inc.IncrementalCommon.recompileClasses(IncrementalCommon.scala:180)  
at sbt.internal.inc.IncrementalCommon.cycle(IncrementalCommon.scala:98)  
at sbt.internal.inc.Incremental$.$anonfun$compile$3(Incremental.scala:102)  
at sbt.internal.inc.Incremental$.manageClassfiles(Incremental.scala:155)  
at sbt.internal.inc.Incremental$.compile(Incremental.scala:92)  
at sbt.internal.inc.IncrementalCompile$.apply(Compile.scala:75)  
at sbt.internal.inc.IncrementalCompilerImpl.compileInternal(IncrementalCompilerImpl.scala:348)  
at sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileIncrementally$1(IncrementalCompilerImpl.scala:301)  
at sbt.internal.inc.IncrementalCompilerImpl.handleCompilationError(IncrementalCompilerImpl.scala:168)  
at sbt.internal.inc.IncrementalCompilerImpl.compileIncrementally(IncrementalCompilerImpl.scala:248)  
at sbt.internal.inc.IncrementalCompilerImpl.compile(IncrementalCompilerImpl.scala:74)  
at io.gatling.compiler.ZincCompiler$.doCompile(ZincCompiler.scala:211)  
at io.gatling.compiler.ZincCompiler$.delayedEndpoint$io$gatling$compiler$ZincCompiler$1(ZincCompiler.scala:216)  
at io.gatling.compiler.ZincCompiler$delayedInit$body.apply(ZincCompiler.scala:39)  
at scala.Function0.apply$mcV$sp(Function0.scala:39)  
at scala.Function0.apply$mcV$sp$(Function0.scala:39)  
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)  
at scala.App.$anonfun$main$1$adapted(App.scala:80)  
at scala.collection.immutable.List.foreach(List.scala:392)  
at scala.App.main(App.scala:80)  
at scala.App.main$(App.scala:78)  
at io.gatling.compiler.ZincCompiler$.main(ZincCompiler.scala:39)  
at io.gatling.compiler.ZincCompiler.main(ZincCompiler.scala)  
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)  
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
at java.base/java.lang.reflect.Method.invoke(Method.java:564)  
at io.gatling.mojo.MainWithArgsInFile.runMain(MainWithArgsInFile.java:50)  
at io.gatling.mojo.MainWithArgsInFile.main(MainWithArgsInFile.java:33)

---

<div class="post-metadata">

**Author:** ![Sri\_Harsha\_G](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@Sri\_Harsha\_G](https://community.gatling.io/u/Sri_Harsha_G)\
**Post date:** [May 24, 2021, 3:23pm UTC](https://community.gatling.io/t/using-andthen-in-the-simulation/6111/7 "2021-05-24T15:23:21Z")

</div>

I see a type mismatch error in the stack trace you printed. Please check that.  
I dont see any issues with the usage of andThen.  
Also you may have to check the gatling version \> 3.4.0

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [May 24, 2021, 5:30pm UTC](https://community.gatling.io/t/using-andthen-in-the-simulation/6111/8 "2021-05-24T17:30:59Z")

</div>

Yes, compiles on my side too, so I agree, it’s likely you’re using an outdated version of Gatling from before the “andThen” feature was introduced.  
[https://gatling.io/docs/current/whats\_new/3.4/](https://gatling.io/docs/current/whats_new/3.4/)

---

<div class="post-metadata">

**Author:** ![gkandale](https://avatars.discourse-cdn.com/v4/letter/g/c2a13f/32.png) [@gkandale](https://community.gatling.io/u/gkandale)\
**Post date:** [May 25, 2021, 9:27am UTC](https://community.gatling.io/t/using-andthen-in-the-simulation/6111/9 "2021-05-25T09:27:24Z")

</div>

Thank you Sri and Stéphane. I appreciate your straight forward responses. I will check version.

Thank you!  
GK

---

<div class="post-metadata">

**Author:** ![gkandale](https://avatars.discourse-cdn.com/v4/letter/g/c2a13f/32.png) [@gkandale](https://community.gatling.io/u/gkandale)\
**Post date:** [May 25, 2021, 7:31pm UTC](https://community.gatling.io/t/using-andthen-in-the-simulation/6111/10 "2021-05-25T19:31:53Z")

</div>

As you suspected, it was the version number, so that is all set now, thank you very much. One last question, are the session variables created in the the parent scenario available to the child scenarios too when using .andThen? Or are they gone once the parent scenario finishes?

-GK

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [May 25, 2021, 7:34pm UTC](https://community.gatling.io/t/using-andthen-in-the-simulation/6111/11 "2021-05-25T19:34:07Z")

</div>

Those are different scenarios with distinct injection profiles, hence distinct virtual users.  
So nothing shared unless you implement something yourself.

---

<div class="post-metadata">

**Author:** ![Sri\_Harsha\_G](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@Sri\_Harsha\_G](https://community.gatling.io/u/Sri_Harsha_G)\
**Post date:** [May 25, 2021, 8:13pm UTC](https://community.gatling.io/t/using-andthen-in-the-simulation/6111/12 "2021-05-25T20:13:16Z")

</div>

George,  
This is what we did for our performance tests.

1. Created a parent CSV feeder
2. Set the data in the feeder in the session object.
3. Used the session object to feed the data in the request for the api. Before that do a check doIf(session =\> Option(“request\_data”).forall(session(\_).asOption[String].nonEmpty))
4. Used injection profile to run the tests

---

<div class="post-metadata">

**Author:** ![gkandale](https://avatars.discourse-cdn.com/v4/letter/g/c2a13f/32.png) [@gkandale](https://community.gatling.io/u/gkandale)\
**Post date:** [May 25, 2021, 8:21pm UTC](https://community.gatling.io/t/using-andthen-in-the-simulation/6111/13 "2021-05-25T20:21:50Z")

</div>

Thank you Sri! I appreciate your response. Will give that a go. I’ll let you know how it goes.

-GK

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [May 26, 2021, 7:28am UTC](https://community.gatling.io/t/using-andthen-in-the-simulation/6111/14 "2021-05-26T07:28:05Z")

</div>

Also, kind reminder of the requirements on this group: [https://groups.google.com/g/gatling](https://groups.google.com/g/gatling)

- **Make sure you’re using an up-to-date Gatling version**
