BreadcrumbHomeResourcesBlog Using The While Controller In JMeter June 18, 2021 Using the While Controller in JMeterOpen Source AutomationBy Dmitri TikhanskiDo you know all about the JMeter While Controller? Get an overview and find out when to use it. Table of Contents:About the While Controller in JMeterUsing Functions in the While ControllerPotential Use CasesAbout the While Controller in JMeterWhen it comes to building an advanced JMeter load test scenario that assumes complex logic, depending on certain conditions it is sometimes required to change the Samplers execution order. Usually JMeter runs all the Samplers upside down but there could be some scenarios when the default behaviour is not suitable.This post covers the While Controller - JMeter’s implementation of while loop. For those not familiar with the while loop concept, to put it simply, it’s just a repeated “if->then” statement.If something is true, then repeat the last actionIn JMeter, the controller basically runs children Samplers, unless some predetermined “Condition” is “true” where “Condition” could be:blank - in this case, the While Controller will exit when the last sampler in the loop failsLAST - the same as blank, but with an additional check for the last Sampler before the loop result. If the preceding Sampler is not successful, the While Controller and its children won’t be executed at allJMeter Function or Variable - the While Controller’s children will be run until a variable or function evaluation result is “true”JMeter Property - the same as Function or Variable, but assumes the Property insteadBoth the Function/Variable and Property approaches assume that the “Condition” will be set to “false” somewhere in or outside the loop. In the case of Property, it can be done from another Thread Group or even outside JMeter, for example via the Beanshell Server.The first 2 cases with “blank” and “LAST” are pretty straight-forward. if you leave the “Condition” blank or set it to LAST, the loop will break if the last sampler under the While Controller is not be successful. The same applies to the Variables and Properties. The While Controller will loop its children until the Variable or Property specified in “Condition” will result in “true”.Using Functions in the While ControllerAs per the line in parentheses in the While Controller GUI, the condition should be “function or variable”, as seen below.This means that, unlike the If Controller which automatically interprets the “Condition” as JavaScript, the While Controller expects it to be either “true” or “false”. Therefore:"${myVar}" != "some value" - doesn’t work, it is neither a function nor a single variable${__javaScript("${myVar}" != "some value",)} - works OK as the expression resolves it to either be “true” or “false”Additionally, remember that in the absolute majority of cases, JMeter Variables are stored as Strings, so if you’re using __javaScript() or a similar function in order to compare the variable value to something, make sure that both counterparties are of the same type. For example, in JavaScript just surround both the variable and the expected value with quotation marks and they will be interpreted as strings. Now that we’ve explained how the While Controller is used, let’s take a look at some use cases.Potential Use CasesLooping Until a Response Contains a Specified LineIf you’re testing a system built on, let’s say, the eventual consistency principle or a multi-component system where the response is not immediate, you might have to wait until something becomes available so you can proceed. In these scenarios, the While Controller is the most obvious choice to turn to to. If need to decide whether to continue or not based on the presence of some text in the response - it can be done as follows:Add the While Controller to your Test PlanPut the HTTP Request sampler (or any other sampler if you’re using a different protocol) under the While ControllerSave the partial or the whole response into a JMeter Variable. It can be done using the Regular Expression Extractor.Use the following Condition in the While Controller:${__javaScript("${response}".indexOf("string") == -1,)}Where:${response} - the JMeter Variable which holds partial or whole responsestring - the character sequence you’re looking forOnce the string will be found in the ${response} the loop will exit and the test will continue (or end).If there won’t be any occurrences of the string in the ${response} the request will be repeated forever. The above approach assumes the Regular Expression Extractor and __javaScript() function combination, and in the String.indexOf() the JavaScript method is used to determine whether the search criteria are present in the response or not.Looping Until the Response Contains A Specified Line, But Not More Than X TimesThis use case is similar to the previous one, with some extra “protection”, in order to avoid an endless loop if the text you’re looking for never appears.Let’s retry for 5 times and if the string will not be found in the response, then exit the loop. To implement, you just need to:Add a Counter test element under the While ControllerConfigure the Counter to:Start from 1Increment by 1Have a counter reference nameChange the While Controller Condition to consider the counter value as well, like:${__javaScript(("${response}".indexOf("string") == -1 && ${counter} < 5),)}As you can see, the while loop stopped after 5 attempts instead of running forever. Reading all Values from the CSV and ContinueThis scenario assumes using the CSV Data Set Config for parametrization. In most cases it is utilized for recycling or stopping the JMeter Thread when the end of the file is reached. Now we will see how to read all the values from the CSV file and continue the test afterwards.Assuming that CSV file contains only 5 lines:line 1 line 2 line 3 line 4 line 5And the following CSV Data Set Configuration:Let’s see what happens with the blank Condition in the While Controller.As you can see, the test continues forever and returns the value for each subsequent call. EOF stands for End Of File, if you don’t like it - you can change it for whatever you want to see instead via the csvdataset.eofstring property.So given we have this “” thing, now we can use it in the While Controller Condition as: ${__javaScript("${myVar}" != "",)}Let’s see what happens now:Almost done. I assume you don’t want this bit at all? It is there as the CSV data is being read in the loop before the While Controller condition is evaluated, so that the While Controller is always one step behind. You can get rid of it using the If Controller by the following steps:Put the If Controller under the CSV Data Set ConfigPut all the samplers which use the ${myVar} variable under the If ControllerUse "${myVar}" != "" as the If Controller conditionLet’s see how it looks now:Seems to be exactly what we’re looking for.The funny thing is that assuming you use the If Controller, you can even leave the While Controller’s Condition blank.Using Regex in the While ControllerIf you need to check a variable value against a regular expression here are some useful examples on how to do it.Test( ): Test for a match in the String. Returns true or false.Code: ${__javaScript(/regex/.test("${variable}"))} Match( ): Returns an array containing all of the matches, including capturing groups, or null if no match is found. Code: ${__javaScript("${variable}".match("string"))} endsWith( ): Determines whether a string ends with the characters of a specified string. Code: ${__javaScript("${variable}".endsWith('string'))} Common Errors when Coding with the While ControllerWhen you are working with While Controllers, a common error you can get in the JMeter log is: “org.mozilla.javascript.EvaluatorException” (see below). To solve this error, check your code syntax and look for any missing double quotes, parentheses and curly braces.Pro tip: Although it is possible to use Beanshell on While Controllers, it’s recommended to use JavaScript functions. They are is more efficient than Beanshell.This is it on the While Controller, if anything remains unclear or your scenario is not covered, feel free to use comments form below.Learn more advanced JMeter at BlazeMeter University.START TESTING NOW Related Resources: What is JMeter’s Transaction Controller?How to Use a Counter in a JMeter TestJMeter’s Weighted Switch Controller - The Ultimate GuideHow to Use the Parallel Controller in JMeter
Dmitri Tikhanski Contributing Writer Dmitri Tikhanski is a Contributing Writer to the BlazeMeter blog.