BreadcrumbHomeResourcesBlog How To Use The JMeter ForEach Controller June 6, 2024 How to Use the JMeter ForEach ControllerPerformance TestingBy Dmitri TikhanskiFor testers, finding efficiencies and ways to save time throughout the development process must always be a priority. One of the ways in which a tester can do this is through the use of “foreach loops.” Foreach loops provide a contained and organized way to execute commands against a section of code — avoiding “off-by-one” errors and making the code easier to read.JMeter’s ForEach Controller is a tool that allows you to implement foreach loops in your JMeter tests.In this blog, we will discuss what JMeter’s ForEach Controller is and provide a demo on how to use it.Table of ContentsHow to Use JMeter’s ForEach ControllerTop JMeter ForEach Controller Tips Bottom LineTable of Contents1 - How to Use JMeter’s ForEach Controller2 - Top JMeter ForEach Controller Tips 3 - Bottom LineBack to topHow to Use JMeter’s ForEach ControllerImagine this situation: You need to execute the same action multiple times — such as visiting all links on a web page or booking a flight from all possible airports. Instead of copying and pasting the request multiple times (i.e. DRY principle) or manually saving the list of links or destinations, you can dynamically obtain the list and save it into JMeter Variables.You can then let the ForEach Controller run a Sampler (or several) for each variable in the list.Here is a simple example. Our BlazeMeter Demo page (you can use it safely for your experiments with JMeter/BlazeMeter) represents a very simple,basic travel agency.We will come up with a simple JMeter test plan that will search for flights from Paris to all possible destinations using JMeter’s ForEach Controller.Step 1: Open the Main PageFor simulating a user who opened the main page of our travel agency, add a HTTP Request sampler to the Thread Group and configure like this:Step 2: Get All Possible DestinationsNext, we need to obtain all destination airports that are present in the dropdown menu. The best to do this is by using CSS Selector Extractor. It enables accessing HTML elements using CSS Selectors and it is much simpler than doing this with Regular Expressions. Let us see how the destination dropdown menu looks in browser developer tools:Now copy the HTML Select tag code below for a better experience:<select name="toPort" class="form-inline"> <option value="Buenos Aires">Buenos Aires</option> <option value="Rome">Rome</option> <option value="London">London</option> <option value="Berlin">Berlin</option> <option value="New York">New York</option> <option value="Dublin">Dublin</option> <option value="Cairo">Cairo</option> </select>Here we can get the options list by simple CSS selector expression: select[name=toPort] option and use value as the attribute and the relevant CSS Selector Extractor configuration would be:We will add a Debug Sampler and View Results Tree listener see how it works:As you can see, we now have the following JMeter Variables defined:destination_1=Buenos Aires destination_2=Rome destination_3=London destination_4=Berlin destination_5=New York destination_6=Dublin destination_7=Cairo destination_matchNr=7This is exactly what the ForEach Controller expects: variables with _1, _2, etc. postfixes.Step 3: Implementing Foreach LoopNow we are ready to use the variables from the previous step in ForEach Controller and look for flights from Paris to all possible destinations.Add a ForEach Controller instead of Debug Sampler, and configure it to read the values with destination prefix and write the current value into toPort variable:This basic setup will iterate all seven destination ports from our CSS Selector Extractor so ForEach Controllers child(ren) will be executed seven times —each time toPort variable will take the next value of destination_ variable.Back to topTop JMeter ForEach Controller Tips Current Iteration NumberForEach Controller exposes its current iteration number through a special, pre-defined variable in the form of ${__jm__name-of-foreach-controller-here__idx}. If you didn’t change the default name of the ForEach Controller, it will be ${__jm__ForEach Controller__idx} — just in case you want to determine the current iteration number and break the loop with the help of If Controller and Flow Control Action Sampler.Start and End Indices For LoopBy default, ForEach Controller starts counting from 0 (and doesn’t include 0, so the first variable will be 1) and continues until variables are available. However, if you want to use a subset of variables (i.e. from London to Dublin inclusively) you need to set:Start index for loop: 2 (London is destination_3 so we need to subtract 1)End index for loop: 6 (Dublin is destination_6)Add “_” Before Number This setting instructs the ForEach Controller which pattern of variables scanning to use, CSS Selector Extractor and other Post-Processors return variables in the form of match_1, match_2, and so on. But if your variables are coming from a different source and do not have this underscore, you can uncheck the box and you will not need to manipulate the variables names in order to feed them to the ForEach Controller.Back to topBottom LineJMeter’s ForEach Controller is a great feature to help create more efficiencies in your JMeter tests. Rather than manually apply commands over and over again, users can automate these command applications based on the variables provided.While JMeter is a great load testing tool in its own right — especially for testing fields with multiple variables — it does have its shortcomings. By executing your JMeter tests with BlazeMeter, you can radically elevate the power and efficacy of your load tests.Get started using the JMeter ForEach Controller tool with BlazeMeter for FREE today!Start Testing NowBack to top
Dmitri Tikhanski Contributing Writer Dmitri Tikhanski is a Contributing Writer to the BlazeMeter blog.