2
votes

I have this header in a csv file: TEST_ID;TEST_DESC;RQ_FIELD1;RQ_FIELD2;RQ_FIELD3;FIELD4;FIELD5;FIELD6;FIELD7;FIELD8

and I'm using this declaration to read its values from csv in a Jmeter Groovy:

String[] parameters = new String[]{vars.get("FIELD1"),vars.get("FIELD2"),vars.get("FIELD3")};

Then I used the values from vars.get to create my JSON request.

Since the header could change based on the csv structure, I would like to create something dynamic, instead of use declaration of String[] parameters = new String[]{vars.get("FIELD1"),vars.get("FIELD2"),vars.get("FIELD3")};

Then I wrote this:

public static String getFirstLine() throws IOException {
    BufferedReader br = new BufferedReader(new FileReader(props.get("csvFilePath")));
    String line = br.readLine();
    br.close();
    return line;
}

    String line=getFirstLine();
    String[] header = new String[20];
    String[] parameters;
    int j=1;
    try {
        header = line.split(";");
        for (int i=1;i<header.length;i++){
          if (header[i].toString().contains("RQ_")){
            j++;
          }
        }
        parameters = new String[j];
        j=1;
        for (int i=1;i<header.length;i++){
          if (header[i].toString().contains("RQ_")){
            header[i] = header[i].toString().replaceAll("RQ_", "");
            header[i] = header[i].toString().trim();

            if (j!=parameters.length-1){
              parameters[j] = "vars.get(\""+header[i].toString()+"\"),";
            }
            else {


    parameters[j] = "vars.get(\""+header[i].toString()+"\")";
        }

        log.info ("====parameters.length["+parameters.length+"====header.length["+header.length+"]====i["+i+"]====j["+j+"]=====paramters["+parameters[j]+"]");
        j++;
      }

    }
    for (p=1;p<parameters.length;p++){
      log.info ("=====paramters["+parameters[p]+"]");
    }
  }catch (Exception e) {
  log.info(e.toString());
  log.info(result.sampleEnd());

}

the output is:

INFO o.a.j.m.JSR223PreProcessor: ====parameters.length[4====header.length[10]====i[2]====j[1]=====paramters[vars.get("FIELD1"),]
INFO o.a.j.m.JSR223PreProcessor: ====parameters.length[4====header.length[10]====i[3]====j[2]=====paramters[vars.get("FIELD2"),]
INFO o.a.j.m.JSR223PreProcessor: ====parameters.length[4====header.length[10]====i[4]====j[3]=====paramters[vars.get("FIELD3")]

Now I would like to use array parameters to read the values of csv file:

String key;
for (String value : parameters) {
    key = "";
    if (value.length() > 0) {
    log.info("value["+value+"]);
    }
}

I got an error

javax.script.ScriptException: Sourced file: eval stream : Can't iterate over type: class bsh.Primitive : at Line: 81 : in file: eval stream : for ( String value : parameters ) { 
 in eval stream at line number 81
    at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:93) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.engine.BshScriptEngine.eval(BshScriptEngine.java:52) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:212) ~[?:1.8.0_181]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:199) ~[ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.modifiers.JSR223PreProcessor.process(JSR223PreProcessor.java:44) [ApacheJMeter_components.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:849) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:467) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:416) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:250) [ApacheJMeter_core.jar:4.0 r1823414]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_181]
2
it's possible to simplify the code in groovy... - daggett
@daggett how? any suggestion? - ClaudioM
i put an example in answer - daggett

2 Answers

1
votes

From the error BshScriptEngine you are using Beanshell/Java language.

For groovy, change language field to groovy in JSR223 element

0
votes

That's not an answer to the question.

That's just a sample how you could read simple CSV file in groovy:

def headers=null
new File('./test.csv').eachLine("UTF-8"){line->
    if(!headers){
        //split heaeders and do some modifications on header keys
        headers = line.split(';').collect{ it.replaceAll("RQ_","").trim() }
    }else{
        //split line to values
        def values = line.split(';')
        //convert values to map(header->value) to be able to access value by header
        def row = values.toList().withIndex().collectEntries{e-> [ headers[e[1]], e[0] ] }
        //at this point it's possible get value by header name
        log.info( row.FIELD1 )
    }
}

note that a lot of java classes has additional methods in groovy: http://docs.groovy-lang.org/latest/html/groovy-jdk/

for example array of objects has a method collect(Closure)

that's why it's possible to write:

line.split(';').collect{ ... }