# Looping in Gatling

**URL:** <https://community.gatling.io/t/looping-in-gatling/827>\
**Category:** Gatling (Open-Source)\
**Created:** [February 19, 2014, 11:00pm UTC](https://community.gatling.io/t/looping-in-gatling/827 "2014-02-19T23:00:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![davids](https://avatars.discourse-cdn.com/v4/letter/d/65b543/32.png) [@davids](https://community.gatling.io/u/davids)\
**Post date:** [February 19, 2014, 11:00pm UTC](https://community.gatling.io/t/looping-in-gatling/827/1 "2014-02-19T23:00:08Z")

</div>

Hey Folks,

I am really new at Gatling so please don’t crucify me. I am tried to read thorough the wiki docs. I am using gatling 2.

I am just trying to test and application that sends 3 posts after I do a get. After each post more and more data is returned. I can’t tell that if once the application is under a lot of load if the application will only send 3 posts. I assume it will keep sending them as long as it need to collect the data.

I am trying to run a report and there is an ajax call that happens. So I noticed that every 30 seconds there is a new post that is returned.  
Therefore, what I want to do is keep executing the post until I find the word “test” in the body of the post.

Is something like this possible with Gatling?  
.doWhile("${myvalue}" does not contain “test”) {  
exec(http(“Post”))  
once I find “test” exit loop  
}

---

<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:** [February 20, 2014, 5:32am UTC](https://community.gatling.io/t/looping-in-gatling/827/2 "2014-02-20T05:32:03Z")

</div>

> I am really new at Gatling so please don't crucify me.

Never, we don't do that here 🙂

Assuming you want to continue looping if myvalue doesn't exist:

.asLongAs(session =\> session("myvalue").asOption[String].map(myValue =\>  
!myValue.contains("test")).getOrElse(true)) {  
&nbsp;&nbsp;exec(http("Post"))  
}

---

<div class="post-metadata">

**Author:** ![Floris\_Kraak](https://avatars.discourse-cdn.com/v4/letter/f/848f3c/32.png) [@Floris\_Kraak](https://community.gatling.io/u/Floris_Kraak)\
**Post date:** [February 20, 2014, 9:17am UTC](https://community.gatling.io/t/looping-in-gatling/827/3 "2014-02-20T09:17:34Z")

</div>

Yes. Crucifiction is simply too slow and inefficiënt. We are much more modern: Guillotines are more the gatling style.

😛

---

<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:** [February 20, 2014, 9:20am UTC](https://community.gatling.io/t/looping-in-gatling/827/4 "2014-02-20T09:20:20Z")

</div>

French touch!

Nice one 🙂

---

<div class="post-metadata">

**Author:** ![davids](https://avatars.discourse-cdn.com/v4/letter/d/65b543/32.png) [@davids](https://community.gatling.io/u/davids)\
**Post date:** [February 20, 2014, 10:53pm UTC](https://community.gatling.io/t/looping-in-gatling/827/5 "2014-02-20T22:53:17Z")

</div>

> @Stéphane  
> Thank you so much for your help. I had to make a small change.  
> Instead of:  
> .asLongAs(session =\> session(“myvalue”).asOption[String].map(myValue =\> !myValue.contains(“test”)).getOrElse(true)) {  
> I had to use:  
> .asLongAs(session =\> session(“myvalue”).asOption[String].map(myValue =\> !myValue.contains(“test”)).getOrElseBoolean) {  
> I was getting a compilation error.  
> It works as expected.  
> @Floris  
> Nice one indeed! 🙂  
>   
>   
> |

---

<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:** [February 21, 2014, 11:37am UTC](https://community.gatling.io/t/looping-in-gatling/827/6 "2014-02-21T11:37:55Z")

</div>

We’re reaching the limits of the Scala compiler type inference here.

The reason is that asLongAs actually expects a Session =\> Validation[Boolean], not a Session =\> Boolean.

There’s an implicit converting from Foo to Validation[Foo], but the compiler doesn’t seem to be able to revolve this implicit AND infer the getOrElse type parameter.

With current snapshot, you could write:  
.asLongAs(session =\> session(“myvalue”).validate[String].map(myvalue =\> !myvalue.contains(“test”)).recover(true)) {

}

---

<div class="post-metadata">

**Author:** ![Rohit\_Shinkar](https://avatars.discourse-cdn.com/v4/letter/r/3ec8ea/32.png) [@Rohit\_Shinkar](https://community.gatling.io/u/Rohit_Shinkar)\
**Post date:** [May 24, 2021, 6:58am UTC](https://community.gatling.io/t/looping-in-gatling/827/7 "2021-05-24T06:58:32Z")

</div>

is it possible to add multiple string validations inside the asLongAs ?  
scenario-\>

1. call the api, read the result -  
if the result is either of (abc) or (xyz) then exit if not keep calling the api ?
