# Dynamically adding scenarios based on loop

**URL:** <https://community.gatling.io/t/dynamically-adding-scenarios-based-on-loop/7192>\
**Category:** Gatling (Open-Source)\
**Created:** [October 3, 2022, 9:17am UTC](https://community.gatling.io/t/dynamically-adding-scenarios-based-on-loop/7192 "2022-10-03T09:17:55Z")\
**Posts on this page:** 1\
**Showing post:** 6

<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:** [October 3, 2022, 11:03am UTC](https://community.gatling.io/t/dynamically-adding-scenarios-based-on-loop/7192/6 "2022-10-03T11:03:10Z")

</div>

Obviously, we won’t help with your jenkins part.

To reduce verbosity (as I don’t think that is the subject at hand), let’s start with this code:

```scala
class Sample extends Simulation {
  private val httpProtocol = http
    .baseUrl("https://sample.com")

  val scnLaunchApp = scenario("LaunchApp").exec(
    // ...
  )
  val scniosDeviceApps = scenario("iosDeviceApps").exec(
    // ...
  )

  setUp(
      scnLaunchApp.inject(atOnceUsers(1)),
      scniosDeviceApps.inject(atOnceUsers(1)),
    ).protocols(httpProtocol)
}

```

Do you know that `setUp` may take a `List[PopulationBuilder]` as arguments?

```scala
  val injections = List(
    scnLaunchApp.inject(atOnceUsers(1)),
    scniosDeviceApps.inject(atOnceUsers(1)),
  )

  setUp(injections).protocols(httpProtocol)

```

Do you know that you can build a list of elements that are optional?

```scala
val sample = List(
  Some(1),
  None,
  Some(2),
  None,
  None,
  Some(3)
)
// val sample: List[Option[Int]] = List(Some(1), None, Some(2), None, None, Some(3))

```

Then, you can `flatten` it

```scala
sample.flatten
// res: List[Int] = List(1, 2, 3)

```

In another hand, you can get an option for an environment variable with `sys.env.get("ENV_NAME")` or an option of system property with `sys.props.get("prop.name")`.

So, you can write something like:

```scala
  val injections = List(
    sys.props.get("launchApp").filter(_ == "true").map(_ => scnLaunchApp.inject(atOnceUsers(1))),
    sys.props.get("iosDeviceApps").filter(_ == "true").map(_ => scniosDeviceApps.inject(atOnceUsers(1))),
  ).flatten

  setUp(injections).protocols(httpProtocol)

```

To achieve what you want.

Is that clear?

I (really do) hope that helps.

Cheers!

---

_[View the full topic](https://community.gatling.io/t/dynamically-adding-scenarios-based-on-loop/7192)._
