Skip to content

While Action

While until

The While Action runs its children until the condition is "false". The condition is evaluated twice, once before executing the actions below it, and once at end of children execution.

Warning

Be careful since only children of the While Action will be impacted. Use the indentation to check that your While Action is correctly setup.

Parameter name Description Required
Name Descriptive name for this container which is indicated in the bench report. The name is not required, but it is a good habit to name your containers so that you can differentiate them in your bench reports. No
Condition This condition must be a variable or function whose value is "true" in order continue processing the children of the While action. If none specified, the result of the last sampler will be used instead. Yes

While conditions

While condition

Similarly to the If Action, the while conditions must have a direct "true" or "false" value. Because of this, you cannot directly compare two values. Instead you must either insert a variable or use a script like this one:

${__groovy(${count}<=2)}
In this example we take the count variable and evaluate if it is less or equal than two.


In order to make your life easier, you can activate our condition builder and avoid syntax errors. Check the condition builder documentation for more info.

While examples

With counter

While counter

While with loop

Before the loop it is important to initialize our counter with a first JSR223 script:

vars.put("counter","1");

Now add a WHILE logic action and in the condition, to just limit the number of iterations you can use:

${__groovy(${counter}<=5)}
Just replace 5 by the number of iterations you want.

And now inside the while add another script to increment the counter, make sure to use groovy as a language for this one:

int counter = vars.get("counter").toInteger()+1;
vars.put("counter",counter.toString());

Tip

You can download a JMX of this example here and import it in your project using the JMeter project option.

String compare

While until

While with string comparison

Here is another example with string comparison, we are running the loop while myvar contains something else than FINISHED:

${__groovy(!"${myvar}".equals("FINISHED"))}
Ideally myvar would be a regex extracting a status from one of the responses inside the loop. This can be used to handle an asynchronous task and wait for feedback by reloading the same page.

Tip

You can download a JMX of this example here and import it in your project using the JMeter project option.

For each

While foreach

While foreach

In this other case we continue the loop until categories_n does not exists:

${__groovy(vars.get("categories_"+vars.get("counter")) != null)}
Note that if categories is a regexp extracting all occurrences, you can also check that counter is less than the matchNr variable:
${__groovy(${counter} <= ${categories_matchNr})}

Now assuming you named your variables the same way, inside the while you can go with the following syntax to use the current category: ${__V(categories_${counter})}

Tip

You can download a JMX of this example here and import it in your project using the JMeter project option.