2
votes

I'm trying to make a BeanShell script within JMeter to create an XML message and save it as a variable (and then have a SOAP sampler send the message of course, but that's not really the point). Within the script, I have a loop that randomly creates a few lines, and the point is that every loop the script reads a new line from a CSV file. The loop works fine, but for some reason, the CSVread next doesn't seem to work so every time it just uses the same line from the CSV file.

Here is the code I'm using:

try{

  //Use a random variable to loop for a random amount of times
  randomnr = vars.get("randomvariable");
  int randomint = Integer.parseInt(randomnr);
  int FiNr = 123456789;

  //I'm using a stringbuilder to create the xml message
  StringBuilder multi =  new StringBuilder();

  for (int i=1; i<randomint; i++){
    multi.append("SomeXML");
    FiNr = ${__CSVRead(data.csv,0)}; //get a line from the csv
    multi.append(""+FiNr);
    multi.append("SomeMoreXML");
    ${__CSVRead(data.csv,next)}; //Go to the next line in the csv - doesn't seem to be working
  }

  vars.put("xmlmessage",multi.toString()); //put all of it in a variable

}
catch(Exception ex){
  log.warn("Something bad happened", ex);
  throw ex;
}

Everything is working except that it's not advancing to the next line within the loop. Any thoughts?

1
I'm unfamiliar with this function, so I'd prefer not to leave this as an answer, but I would assume that you want int FiNr = ${__CSVRead(data.csv,0)}; outside of your for loop and ${__CSVRead(data.csv,next)}; should be FiNr=${__CSVRead(data.csv,next)};.RowlandB
@RowlandB Thanks for pointing that out, it was a rookie programming mistake. Unfortunately it didn't solve the problem with the csv file. I updated the code. Also, FiNr=${__CSVRead(data.csv,next)}; doesn't work because you need to specify the column for the function to return a value.Ruben van Kruistum

1 Answers

0
votes

Unfortunately, _csvread() function will not work with loop inside the Beanshell sampler. Try to implement jmeter Loop controller and add __CSVread() function under that.