# Session variable question

**URL:** <https://community.gatling.io/t/session-variable-question/50>\
**Category:** Gatling (Open-Source)\
**Created:** [March 12, 2012, 2:01pm UTC](https://community.gatling.io/t/session-variable-question/50 "2012-03-12T14:01:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Espen\_Nersveen](https://avatars.discourse-cdn.com/v4/letter/e/50afbb/32.png) [@Espen\_Nersveen](https://community.gatling.io/u/Espen_Nersveen)\
**Post date:** [March 12, 2012, 2:01pm UTC](https://community.gatling.io/t/session-variable-question/50/1 "2012-03-12T14:01:33Z")

</div>

Hi. I’ve been using Gatling for the past week and I’m a bit stuck at what seems to be a simple issue. I’m new to scala.

I basically want to know if a session variable is set or not. The code I’m working on looks like this at the moment:

.exec((s: Session) =\>  
if (s.getAttribute(“myVariable”) != null)  
println(“The variable is set”)  
)

What can I change “!= null” with, to make this code print “The variable is set” when it is in fact set, and do nothing when it is not?

For context, this is my scenario: “Call A” precedes “Call B” and “Call B” is dependent on information in the response of “Call A”. I want to stop “Call B” from executing if “Call A” failed (or returned non useable data).

Sincerely  
Espen Nersveen

---

<div class="post-metadata">

**Author:** ![Romain\_Sertelon](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@Romain\_Sertelon](https://community.gatling.io/u/Romain_Sertelon)\
**Post date:** [March 12, 2012, 2:15pm UTC](https://community.gatling.io/t/session-variable-question/50/2 "2012-03-12T14:15:47Z")

</div>

Hi Espen,

As you can see here ([https://github.com/excilys/gatling/blob/1.0-maintenance/gatling-core/src/main/scala/com/excilys/ebi/gatling/core/session/Session.scala#L63](https://github.com/excilys/gatling/blob/1.0-maintenance/gatling-core/src/main/scala/com/excilys/ebi/gatling/core/session/Session.scala#L63)), the returned result of the getAttribute function if there is nothing found is EMPTY (or “”, the empty string) 🙂

So you should test against “” instead:

if (s.getAttribute(“myVariable”) != “”)

However, I think this will change in 1.1, not sure though 😉

Hope this helps 🙂

Cheers,  
Romain

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [March 12, 2012, 2:28pm UTC](https://community.gatling.io/t/session-variable-question/50/3 "2012-03-12T14:28:00Z")

</div>

Hi Espen,

Would it be possible for you to use the 1.1.0-SNAPSHOT (as explained in [the thread I just opened](https://groups.google.com/group/gatling/browse_thread/thread/5f556e882a806d5b?hl=fr))?

In 1.1.0, here’s what your function should look like :

.exec((s: Session) =\>  
if (s. **isAttributeDefined** (“myVariable”))  
println(“The variable is set”)  
s  
)

Cheers,

Steph

2012/3/12 Espen Nersveen \<[espen.nersveen@gmail.com](mailto:espen.nersveen@gmail.com)\>

---

<div class="post-metadata">

**Author:** ![Espen\_Nersveen](https://avatars.discourse-cdn.com/v4/letter/e/50afbb/32.png) [@Espen\_Nersveen](https://community.gatling.io/u/Espen_Nersveen)\
**Post date:** [March 12, 2012, 2:42pm UTC](https://community.gatling.io/t/session-variable-question/50/4 "2012-03-12T14:42:15Z")

</div>

Thank you for a quick answer.

As for running 1.1.0-SNAPSHOT. I have a parallell task trying to get it to run on that version, but so far not completed. Some issues still remain on cookie and session handling. I will redirect my efforts on converting to 1.1.0-SNAPSHOT.

Thank you  
Espen Nersveen

---

<div class="post-metadata">

**Author:** ![Espen\_Nersveen](https://avatars.discourse-cdn.com/v4/letter/e/50afbb/32.png) [@Espen\_Nersveen](https://community.gatling.io/u/Espen_Nersveen)\
**Post date:** [March 12, 2012, 2:44pm UTC](https://community.gatling.io/t/session-variable-question/50/5 "2012-03-12T14:44:46Z")

</div>

Thanks for the quick answer.

I have tried to check for an empty String and this is the result:  
15:35:38.704 [akka:event-driven:dispatcher:global-11][WARN] c.e.e.g.c.s.Session - No Matching Attribute for key: ‘myVariable’ in session

1.1 has another version, but I’m not able to run on 1.1 just yet. Some issues still remain.

Thank you  
Espen Nersveen

---

<div class="post-metadata">

**Author:** ![Romain\_Sertelon](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@Romain\_Sertelon](https://community.gatling.io/u/Romain_Sertelon)\
**Post date:** [March 12, 2012, 2:52pm UTC](https://community.gatling.io/t/session-variable-question/50/6 "2012-03-12T14:52:24Z")

</div>

Hi Espen,

The warning is not harmful and will not prevent you from running your simulations. The getAttribute method was intended for what it is named: getting the value of an attribute.

This is why an isAttributeDefined method has been created in 1.1 🙂

As for your concerns about migrating to 1.1, what issues are you talking about?

Cheers,  
Romain

---

<div class="post-metadata">

**Author:** ![Espen\_Nersveen](https://avatars.discourse-cdn.com/v4/letter/e/50afbb/32.png) [@Espen\_Nersveen](https://community.gatling.io/u/Espen_Nersveen)\
**Post date:** [March 12, 2012, 3:11pm UTC](https://community.gatling.io/t/session-variable-question/50/7 "2012-03-12T15:11:17Z")

</div>

I have to delve a bit deeper into the debug-logged script to find out  
what is wrong. I'll try to get it up and running on 1.1.0-SNAPSHOT now  
and get back to you when I have more information. I will leave the  
1.0.3 version for now.

Thanks for the help  
espen
