Skip to content

While Action

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

Contrary 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:

${__javaScript(parseInt(vars.get("count"))<=2)}

In this example we take the count variable and evaluate if it is less or equal than two.

While examples

With counter

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:

${__javaScript(parseInt(vars.get("counter"))<=5)}

Just replace 5 by the number of iterations you want.

And now inside the while add another script to increment the counter:

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

String compare

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

${__javaScript(!vars.get("myvar").equals("RUNNING")}

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.

For each

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

${__javaScript(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:

${__javaScript(parseInt(vars.get("counter")) <= parseInt(vars.get("categories_matchNr"))}