2

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?

3
  • 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)};. Commented Jul 12, 2016 at 14:35
  • @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. Commented Jul 18, 2016 at 11:49
  • Possible duplicate of Parsing a CSV file for a multiple row rows using new Java 8 Streams API Commented Sep 20, 2016 at 18:21

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.