Same Parameter value in each Iteration in JMeter

Problem:

  • How to pass the same parameter value in each iteration from a CSV file?
    OR
  • Each thread should select the same row from the CSV file in each iteration.

Example:

Let’s consider, there are 100 users and only 100 records (rows) in the CSV file. In each iteration, the 1st value from the CSV file should be assigned to User1, 2nd value to User2 etc.

User1: ID1,pass1,item1,product1
User2: ID2,pass2,item2,product2
User3: ID3,pass3,item3,product3
.
.
.
User99: ID99,pass99,item99,product99
User100: ID100,pass100,item100,product100

Solution:

1. Add ‘setUp Thread Group’ to your Test Plan (with 1 thread and 1 iteration)
2. Add ‘BeanShell Sampler’ and paste the following code into the “Script” area:

import org.apache.commons.io.FileUtils;
List lines = FileUtils.readLines(new File("test.csv"));
bsh.shared.lines = lines;

3. Add ‘BeanShell PreProcessor’ as a child of the request where you need to use the values from the CSV file and put the following code into the “Script” area:

int user = ctx.getThreadNum();
String line = bsh.shared.lines.get(user);
String[] tokens = line.split(",");
vars.put("ID", tokens[0]);
vars.put("pass", tokens[1]);
vars.put("item", tokens[2]);
vars.put("product", tokens[3]);

Leave a Comment