# Session and conditional statement

**URL:** <https://community.gatling.io/t/session-and-conditional-statement/6980>\
**Category:** Gatling (Open-Source)\
**Created:** [July 28, 2022, 3:03am UTC](https://community.gatling.io/t/session-and-conditional-statement/6980 "2022-07-28T03:03:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sn02](https://avatars.discourse-cdn.com/v4/letter/s/8e7dd6/32.png) [@sn02](https://community.gatling.io/u/sn02)\
**Post date:** [July 28, 2022, 3:03am UTC](https://community.gatling.io/t/session-and-conditional-statement/6980/1 "2022-07-28T03:03:09Z")

</div>

Hello gatling users,

I have a test case where

1. I am setting session variables to initial values

2. call the grPC method,

3. extract values of variables from response

4. update the session variables with values from response

5. Call the gRPC method until the value of session2 variable becomes 0

How can I achieve this?

---

<div class="post-metadata">

**Author:** ![shoaib42](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/shoaib42/32/844_2.png) [@shoaib42](https://community.gatling.io/u/shoaib42)\
**Post date:** [July 28, 2022, 3:31am UTC](https://community.gatling.io/t/session-and-conditional-statement/6980/2 "2022-07-28T03:31:20Z")

</div>

How about something like this, the value of `session2` will be evaluated at the end of the loop ([Gatling - Scenario](https://gatling.io/docs/gatling/reference/current/core/scenario/#dowhile))

```auto
group("test case")(
  doWhile(session => session("session2").as[Int] != 0)(
     exec(yourGrcpChain) //you extract session1 and session2 in this chain
)

```

---

<div class="post-metadata">

**Author:** ![sn02](https://avatars.discourse-cdn.com/v4/letter/s/8e7dd6/32.png) [@sn02](https://community.gatling.io/u/sn02)\
**Post date:** [July 28, 2022, 6:53am UTC](https://community.gatling.io/t/session-and-conditional-statement/6980/3 "2022-07-28T06:53:21Z")

</div>

Thank you. Your solution helped me get past the first hurdle.

Now I am having issues making use of the session variable in the grpc method

I have tried “#{session2}” but it does not get the session2 value.

what am I doing wrong?

Are there any other way to get the session2 value inside of the grpc method?

---

<div class="post-metadata">

**Author:** ![shoaib42](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/shoaib42/32/844_2.png) [@shoaib42](https://community.gatling.io/u/shoaib42)\
**Post date:** [July 28, 2022, 12:37pm UTC](https://community.gatling.io/t/session-and-conditional-statement/6980/4 "2022-07-28T12:37:36Z")

</div>

Can you give an example of your code, that shows the use of session2. I can’t help much without seeing. I misplaced my crystal ball, trust me it’s been difficult to debug my own code lately 😝

---

<div class="post-metadata">

**Author:** ![sn02](https://avatars.discourse-cdn.com/v4/letter/s/8e7dd6/32.png) [@sn02](https://community.gatling.io/u/sn02)\
**Post date:** [July 28, 2022, 4:31pm UTC](https://community.gatling.io/t/session-and-conditional-statement/6980/5 "2022-07-28T16:31:54Z")

</div>

```
group("TestCase1") {
  exec(
    session => {
      session.set("session1", "xyz")
      session.set("session2", 1)
    })
    doWhile(session => {
      val session2 = session.attributes("session2").asInstanceOf[Int]
      session2 != 0 }) (
     exec(
      grpcMethod("#{session2}")
        .extract(_. responseVariable1.some)(_ saveAs ("session1"))
        .extract(_. responseVariable1.size.some)(_ saveAs ("session2")))
     )
  }

```

grpcMethod is the helped method that passes this session2 variable to the actual gRPC call. It is in passing that session2 variable as a parameter I am facing issues.I think I am not using it the right way as a result instead of replacing with the value of session2, the function passes the exact string  
“#{session2}” thereby failing the grpc call

---

<div class="post-metadata">

**Author:** ![shoaib42](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/shoaib42/32/844_2.png) [@shoaib42](https://community.gatling.io/u/shoaib42)\
**Post date:** [July 28, 2022, 5:14pm UTC](https://community.gatling.io/t/session-and-conditional-statement/6980/6 "2022-07-28T17:14:00Z")

</div>

Change the following

```auto
  exec(
    session => {
      session.set("session1", "xyz")
      session.set("session2", 1) // only this one is set, last statement in a function is the return statement
    })

```

to

```auto
  exec(
    session => {
      session.setAll(("session1", "xyz"),("session2", 1))
    })

```

I’m not too familiar with the grpcMethod, I think it may not evaluate Expressions.
