我在csv文件中有此标头: TEST_ID; TEST_DESC; RQ_FIELD1; RQ_FIELD2; RQ_FIELD3; FIELD4; FIELD5; FIELD6; FIELD7; FIELD8
并且我正在使用此声明从Jmeter Groovy中的csv中读取其值:
String[] parameters = new String[]{vars.get("FIELD1"),vars.get("FIELD2"),vars.get("FIELD3")};
然后我使用vars.get中的值创建我的JSON请求。
由于标头可以根据csv结构进行更改,因此我想创建一些动态的内容,而不是使用String[] parameters = new String[]{vars.get("FIELD1"),vars.get("FIELD2"),vars.get("FIELD3")};
的声明
然后我写了这个:
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());
}
输出为:
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")]
现在,我想使用数组参数读取csv文件的值:
String key;
for (String value : parameters) {
key = "";
if (value.length() > 0) {
log.info("value["+value+"]);
}
}
我遇到错误
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]
答案 0 :(得分:1)
由于错误BshScriptEngine
,您正在使用Beanshell / Java语言。
对于常规,在JSR223元素中将语言字段更改为groovy
答案 1 :(得分:0)
这不是问题的答案。
这只是一个示例,您可以使用Groovy读取简单的CSV文件:
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 )
}
}
请注意,许多Java类在groovy中还有其他方法: http://docs.groovy-lang.org/latest/html/groovy-jdk/
例如对象数组具有方法collect(Closure)
这就是为什么可以写:
line.split(';').collect{ ... }