'How to create a dynamic request variables in JMeter
I have a few values in CSV files. I need to pass this values in JSR223 sampler. if suppose, in CSV files it has 4 variables are present in first row then i need to write two line below.
Example 1 in JSR223 sampler :
('usr', '${Var1}', '${Var2}')
('usr', '${Var3}', '${Var4}')
If 6 variables are present in second row of CSV files, they my script should have three lines.
Example 2 in JSR223 sampler :
('usr', '${Var1}', '${Var2}')
('usr', '${Var3}', '${Var4}')
('usr', '${Var5}', '${Var6}')
My scenario here is, each row will have different count of values would be present. So, how can i create my JSR223 sampler request count based on the values counts that is present in CSV files. May i know how to create this scenario in JMeter.
Solution 1:[1]
Something like:
def lines = new File('test.csv').readLines()
lines.each { line ->
log.info('Line from CSV: ' + line)
def request = new StringBuilder()
def values = line.split(',')
def counter = 0
1.upto(values.size() / 2, { it ->
request.append("('usr',").append(values[counter]).append(", '").append(values[counter + 1]).append(" ')")
if (it != values.size() / 2) {
request.append(System.getProperty('line.separator'))
}
counter = counter + 2
})
log.info('Generated request: ' + System.getProperty('line.separator') + request.toString())
}
should do the trick for you.
Demo:
More information:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dmitri T |

