theartsycoder
theartsycoder

Reputation: 13

JMeter: How to decrement the variable provided for End index field in the Foreach Controller?

I am working on a JMeter test plan where I am extracting various values from a JSON response. The plan requires me to perform a delete operation that relies on dynamic values provided to a ForEach controller. I am using JSON JMESPath to obtain the count of occurrences needed for generating these dynamic values. However, when I use this variable in the ForEach controller's "End index for loop (inclusive)" field, the loop only runs a few times and then fails for the remaining iterations.

Here is an example of the JSON response:

"companyRecords" :[
     {
        "employeeRecord": [
          {
           "employeeid" : "1234",
           "employeename" : "Joe",
           },
          {
           "employeeid" : "5678",
           "employeename" : "Matthew",
           }
          {
          "employeeid" : "1120",
          "employeename" : "David",
           }
          ],
         "joiningMonth" : "April",
         "employmentType" : "FullTime"
       }
 ],

The JMESPath expression in the JMESPath Extractor:

length(companyRecords[].employeeRecord[].employeeid)

The variable name being NoOfRecords. In the current instance, this gives me a count of 3. I am using this as a variable in the Foreach controller as below.

input variable prefix: employeeid
start index for loop (exclusive) : 1
End index for loop (inclusive) : ${NoOfRecords}
Output variable name: employeeid

The above foreach loop is working only for 2 iterations and not the third, I tried with the start index in the JMESPath being set to 0 and still it gives a 403 error.

Is there a way to get this to work? Thanks in advance!

Upvotes: 0

Views: 36

Answers (1)

Ivan G
Ivan G

Reputation: 2907

Your question contains the answer:

start index for loop (exclusive) : 1
End index for loop (inclusive) : ${NoOfRecords}

It will iterate 2 times for 2 and 3 values. so you need to use 0 instead of 1 as the start index for the loop

Also it will be way easier if you use the following JSON JMESPath Extractor configuration:

enter image description here

It will generate the following variables:

employeeid_1=1234
employeeid_2=5678
employeeid_3=1120
employeeid_matchNr=3

so you will be able to setup ForEach Controller as simple as:

enter image description here

Upvotes: 0

Related Questions