JMeter: Need to send an array of random values from a CSV file

Advertisements

I have a CSV file, where topics of interest are present in each row:

List of entries in a CSV file

I want to pick any 4 random values from the CSV file and pass it as an array of strings to the request body, like this:

{"interest": ["BI Modernization", "Salesforce.com / Net Suite", "Data Monetization", "SDN - Software Defined Networks"]}

How can this be achieved?

>Solution :

You can generate the request body using JSR223 PreProcessor and the following code:

def entries = new HashSet()

def linesFromCSV = new File('/path/to/your/file.csv').readLines()

while (entries.size() < 4) {
    entries.add(linesFromCSV.get(org.apache.commons.lang3.RandomUtils.nextInt(0, linesFromCSV.size())))
}

def payload = new groovy.json.JsonBuilder([interest: entries]).toPrettyString()

vars.put('payload', payload)

Generated request body can be referenced as ${payload} where required

More information:

Leave a ReplyCancel reply