How to pass multiple variables in a For Each controller in JMeter

Ruvinda Isuru
Qualityholics
Published in
3 min readJul 7, 2022

Problem

I faced this issue during performance test script creation. The requirement was to pass two dynamic variables to single request repeatedly. You can pass multiple single variables with an API request, but in my case both variables were consisting of nested multiple values. In order to access those values, I used ForEach logic controller. In ForEach controller it facilitates only to have a single input variable.

These are the variables I wanted to pass through the request URL repeatedly

After going through several articles in the internet, I found a solution for the problem

Solution

After configuring ForEach controller , you can access above variables inside ForEach loop using __V() and __Counter() functions. These functions are JMeter inbuilt functions .

For more details you can visit JMeter documentation.

Let’s see how to configure variables to overcome the requirement.

This is the URL I wanted to request repeatedly inside the ForEach loop

URL — www.testwebsite.com/pages/PAGETITLE/PAGEID

PAGETITLE and PAGEID are dynamically changing values.

PAGETITLE — ${PageTitle} — This value is returned by the ForEach controller as the output variable

PAGEID — ${__V(PageIDList_${__counter(,)})}

Then you can configure the URL as

Explanation of the behavior of __V() and __Counter() functions

According to my case , ${PageIDList} variable has nested multiple variables. Let’s assume ${ PageIDList } variable has Page1,Page2,Page3 as it’s nested variable. In order to access nested variables of ${ PageIDList } You have to configure it as below with __V() function.

${__V(PageIDList ${1})} = Page1

${__V(PageIDList ${2})} = Page2

${__V(PageIDList ${3})} = Page3

Let’s look into __counter() function , when user calls __counter() function it generates a new number each time, starting form 1 and incrementing by +1 each time.

For the solution I combined these two functions together and used it in the URL. What happens inside “${__V(PageIDList_${__counter(,)})} “ is , starting from 1 __V() function reads variable according to counter number. For each loop round ,counter number increased by +1 till the end of Foreach loop it runs. That is how __V() reads ${PageIDList} variable with the help of __counter() function .

When you are creating performance test scripts you may face with this kind of problem. In JMeter they have facilitated many solutions as JMeter functions. In this scenario I used only two nested variables to show the configuration part in JMeter script. But you can use this method N number of nested variables in your script. If you can understand the functionality of __V() and __counter() functions’ functionally clearly it will not be a difficult task to overcome.

--

--

Ruvinda Isuru
Qualityholics

I am a Quality Assurance Engineer , currently working at 99x , Sri Lanka