While and If Controller in JMeter

While Loop controller executes 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 the value True and becomes false somewhere else in the script. Once it changes to false, JMeter will exit the While loop.

The Following Example Demonstrates the While Controller in JMeter

  • Open the JMeter in your system and then add Thread Group to the Test Plan(Right-click on Test Plan-->Add-->Thread Group)
  • Next, Add HTTP Sampler Request to the Thread group. (Right-click on Thread Group-->Add-->Sampler-->HTTP Request)
  • Enter the Server details in the HTTP request.
  • Now add View Result Tree to the HTTP Request and then save and run the Test Plan.
  • Now I want to refer the value of URL in a CSV file, for that we need to create a CSV file in your system. I am creating Data.CSV file in my project folder.
  • Open the Data.CSV file and enter the and enter the URL which you have given in the HTTP Request.
  • Next, go to the JMeter and then add the CSV Data Set Config element to the thread group.(Right-click on Thread Group-->Add-->Config Element-->CSV Data Set Config)
  • Next enter the Data.csv file location inside the config element.
  • Next, go to the HTTP request and then add the ${URL} instead of www.google.com, where the URL is the name of the column in the Data.csv file, so from the CSV file we are going to refer the value of the URL.
  • Now Save and run the Test Plan, After that the Request will execute successfully, that means the Request being referenced by our CSV file.
  • Let us add one more URL to the CSV file and then run the Request.
  • Save and Run the thread group.
  • After the Execution, if you check the Request body, you will find that only google URL is executing.
  • To make all the URL to execute from the CSV file, We need to add Loop count which is equal to the number of rows in the CSV file.
  • Go to the Thread Group and enter the Loop count as2.
  • Now if you run the Test Plan, you will find that the two requests will execute and the first time it will execute the First URL and the second time it will execute the Second URL.
  • But It is not the Correct solution to execute all the rows from the CSV file. Because sometimes we need to add more URLs and each time we need to change the Loop count in the Thread Group.
  • Add the While Controller to the Thread group and then move CSV Data Set config and HTTP Request to the While Controller.(Right-click on Thread Group-->Add-->Logic Controllers-->While Controller)
  • Next, enter the ${URL} inside the condition under While Controller.
  • Next, go to the CSV Data Set config and make some changes as:
    • Set Re-Cycle on EOF to False.
    • Set Stop Thread on EOF to True.
  • Now, Save and Run the system and then.
  • Now you can add any number of rows and then run the test plan.

If Controller In JMeter

The If Controller allows the user to control whether the test elements below it (its children) are run or not. By default, the condition is evaluated only once on initial entry, but you have the option to have it evaluated for every runnable element contained in the controller.

The Following Example Demonstrates the If Controller

  • Open the JMeter in your system and then Add Thread Group to the Default Test Plan.
  • Next, add Random Variable to the thread Group.
  • enter the Variable name as random_value inside the Random Variable and enter the minimum value is 1 and the maximum value as 100
  • Next, enter the If Controller to the Thread Group and then add The Condition as ${random_value}<50
  • And also uncheck the Checkbox of Interpret condition as variable expression.
  • Add If controller to the If controller
  • And Add One More Dummy Sampler to the Thread Group.
  • Now, add View Result Tree into the Thread Group.
  • Save and run the test plan, after the execution if you check the request, whenever the value is less than 50 the dummy sampler inside the if the controller will execute otherwise the Dummy sampler which is outside the if the controller will execute.
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions