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)
    adding-http-request-to-sampler-jmeter
  • Enter the Server details in the HTTP request.
    http-request-with-google-address-jmeter
  • Now add View Result Tree to the HTTP Request and then save and run the Test Plan.
    executing-http-request-jmeter
  • 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.
    creating-csv-file-jmeter
  • Open the Data.CSV file and enter the and enter the URL which you have given in the HTTP Request.
    add-google-url-inside-csv-file-jmeter
  • 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)
    add-csv-data-set-config-jmeter
  • Next enter the Data.csv file location inside the config element.
    eneter-csv-file-location-in-csv-config-jmeter
  • 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.
    http-request-with-column-jmeter
  • Now Save and run the Test Plan, After that the Request will execute successfully, that means the Request being referenced by our CSV file.
    http-request-with-successfully-execution-jmeter
  • Let us add one more URL to the CSV file and then run the Request.
    adding-one-more-url-to-the-csv-file-jmeter
  • 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.
    thraed-group-with-loop-count-jmeter
  • 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)
    while-controller-jmeter
  • Next, enter the ${URL} inside the condition under While Controller.
    enter-condition-in-while-loop-controller-jmeter
  • 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.
      set-recycle-and-stop-thread-group-to-false-and-thread-jmeter
  • Now, Save and Run the system and then.
    executing-the-whiel-controller-jmeter
  • 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.
    add-dandom-variable-jmeter
  • enter the Variable name as random_value inside the Random Variable and enter the minimum value is 1 and the maximum value as 100
    random-variable-and-enter-random-value-minimum-value-maximum-jmeter
  • Next, enter the If Controller to the Thread Group and then add The Condition as ${random_value}<50
    Add-if-controller-jmeter
  • And also uncheck the Checkbox of Interpret condition as variable expression.
    uncheck-the-condition-as-variable-expression-jmeter
  • Add If controller to the If controller
    add-dummy-sampler-jmeter
  • And Add One More Dummy Sampler to the Thread Group.
    add-dummy-sampler-to-thread-group-jmeter
  • Now, add View Result Tree into the Thread Group.
    view-result-tree-to-jmeter
  • 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.
    out-put-of-if-controller-jmeter
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions