2

I am working on an script which is using a while loop and I am not able to properly understand the behavior of while loop. like

When does the condition become false...?

From where does it takes inputs, like if sample fails even then the condition becomes false....?

How & What type of conditions can we specify in While. I am not finding any good details of while loop on web

1
  • 1
    You should add your script so we can explain it. In general while loops is executed antil the break condition becomes false - as you said.
    – bish
    Jul 29, 2015 at 12:38

2 Answers 2

3

While Loop controller execute its samplers until the condition specified is not set to False. The condition can be any variable or function that eventually evaluates to the string 'false'.

So, you need to specify a variable or function in While Loop, that has value 'true' and becomes 'false' somewhere else in the script. Once it changes to 'false', JMeter will exit the While loop.

For example if you are using a X-Path extractor in your script which have a variable named Status and its value changes from 'Start' to 'Finish' during the execution and you want to execute your script till 'Finish' has not been met, then you can use the expression ${__javaScript("'${imp_Status}'!='finish'",)} in your While loop and it will execute the samplers under While controller till the status = finish is met.

I have used the same in an eCommerce site, where the condition was that 'User is not allowed to Check-Out products until he purchases items of some amount X' i.e. minimum amount for Check-Out. So we have stored this value of items to be purchased in a Variable and then used the same in While Loop, so that until amount not reached to X, keep of adding products using the samplers which are inside the While loop controller. Once the amount become >= X, the While loop exits and proceeded to Check-Out.

You can refer to below links for more details

https://stackoverflow.com/questions/7886132/jmeter-while-controller

https://stackoverflow.com/questions/30452343/how-to-use-while-loop-in-jmeter

http://jmeter.apache.org/usermanual/component_reference.html#While_Controller

1

While loop condition can have 3 types of value:

Blank : The loop will exit only when at least one of the test element fails. if the failing sample is not the last sample under while controller, then remaining test element would be executed and then loop is exited

LAST : loop will exit only if the last test element under while controller fails. In case any other test element fails, loop will continue.

Condition: This could be any statement of code or a variable. Loop would exit when the value of condition becomes false.

Examples:

  1. ${VAR} - where VAR is set to false by some other test element
  2. ${__javaScript(${C}==10)}
  3. ${__javaScript("${VAR2}"=="abcd")}
  4. ${_P(property)} - where property is set to "false" somewhere else

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.