Control Structure in Apache JMeter - If and While Controller


Control Structure in Apache JMeter - If and While Controller what is if controller and while controller what is their use and how do they work a brief introduction

If Controller

An if controller can be used to control the flow of execution of children. It can be used to start or stop the elements below it depending upon the condition inside its condition column.

There are the following fields inside its control panel.

Name: The name can be anything depending upon the task performed by the controller. It can be used to describe the controller. This field is not mandatory but it would be wise to give a name to the controller , it can be easily lost inside the network of other samplers or controller.

Comment: It can be used to add extra information about the controller. This field is not mandatory either.

Condition (default javascript): This field is main part of the controller. It is mandatory. The condition should be written in this field. The condition can be written as:

	
		${variable}!=10
		“${variable}”== “abcd”
	

The following variable can get values from user defined variables , csv data set config etc.

Evaluate for all children: By clicking on this checkbox, we can make this condition to be checked for all the children under the if controller. This means that if we click the above check box, the condition inside the if controller will be validated for all children before execution.

If Controller example

In this if controller the condition is ${variable}>3 which means the children inside the if controller will run only if the value of variable is greater than 3 . Similarly, we can compare strings or Boolean types.

difference between condition(javascript) and condition as variable expression

if we select interpret condition as variable expression, then the condition will be processed as variable expression rather than requiring javascript. For example, ${variable} will be evaluated as true/ false and compared with true without requiring javascript. If ${variable} is processed with javascript ( default method) then the condition will be evaluated as true/false which would then be passed on to javascript which will return true/false. The condition check, interpreting condition as variable expression , takes lesser time than the default method.

While Controller

This controller continues to run until the condition becomes false in the condition field.

While Controller example

The condition can be any variable or function that eventually evaluates to the string ‘false’ . This allows the use of javascript, Beanshell, properties or variables depending upon the need.

  • Blank - exit loop when last sample in loop fails
  • Last - exit loop when last sample in loop fails. If the last sample just before the loop failed, don't enter loop.
  • Otherwise - exit (or don't enter) the loop when the condition is equal to the string "false"