我正在尝试使用Beah Shell Assertion读取CSV文件。下面是我的bean shell脚本
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
String csvFile =
"C:/Workspace/JmeterProjects/JMeterOutput_${__time(MM-dd-yyyy)}.csv";
BufferedReader bufferedReader = null;
String line = "";
String SEPARATOR = ",";
try {
bufferedReader = new BufferedReader(new FileReader(csvFile));
int counter = 1;
while ((line = bufferedReader.readLine()) != null)
{
String[] items = line.split(SEPARATOR);
print("--------------------LOGGING STARTS-----------------------");
for(int i = 0; i < items.length; i++)
{
log.info(items[i]);
}
print("--------------------LOGGING ENDS-------------------------");
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
但是我说错了
错误o.a.j.u.BeanShellInterpreter:调用bsh方法时出错:eval源文件:内联评估:``import java.io.BufferedReader;导入java.io.FileNotFoundException;导入java。 。 。 ”令牌解析错误:第6行第22列出现词法错误,在:“ \” C:\”之后遇到:“ W”(87)
我不确定这可能是什么错误
答案 0 :(得分:0)
您共享的更新代码可以在完全相同的条件下正常运行,包括Windows系统上的文件路径。仅文件内容不同。请在下面的输出中找到。如果您仍然看到带有更新代码的原始错误,请共享更多信息,例如日志等。
2019-03-29 12:04:34,123 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-1
2019-03-29 12:04:34,137 INFO o.a.j.u.BeanShellTestElement: col1
2019-03-29 12:04:34,137 INFO o.a.j.u.BeanShellTestElement: col2
2019-03-29 12:04:34,138 INFO o.a.j.u.BeanShellTestElement: col3
2019-03-29 12:04:34,138 INFO o.a.j.u.BeanShellTestElement: col4
2019-03-29 12:04:34,138 INFO o.a.j.u.BeanShellTestElement: col5
2019-03-29 12:04:34,138 INFO o.a.j.u.BeanShellTestElement: col6
2019-03-29 12:04:34,138 INFO o.a.j.u.BeanShellTestElement: col7
2019-03-29 12:04:34,138 INFO o.a.j.u.BeanShellTestElement: col8
2019-03-29 12:04:34,138 INFO o.a.j.u.BeanShellTestElement: col9
2019-03-29 12:04:34,138 INFO o.a.j.u.BeanShellTestElement: col10
.
.
.
2019-03-29 12:04:34,170 INFO o.a.j.u.BeanShellTestElement: val25
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.0 r1840935">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
<stringProp name="TestPlan.comments"></stringProp>
<boolProp name="TestPlan.functional_mode">false</boolProp>
<boolProp name="TestPlan.tearDown_on_shutdown">true</boolProp>
<boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
<elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="TestPlan.user_define_classpath"></stringProp>
</TestPlan>
<hashTree>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<stringProp name="LoopController.loops">1</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">1</stringProp>
<stringProp name="ThreadGroup.ramp_time">1</stringProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<stringProp name="ThreadGroup.duration"></stringProp>
<stringProp name="ThreadGroup.delay"></stringProp>
</ThreadGroup>
<hashTree>
<ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="View Results Tree" enabled="true">
<boolProp name="ResultCollector.error_logging">false</boolProp>
<objProp>
<name>saveConfig</name>
<value class="SampleSaveConfiguration">
<time>true</time>
<latency>true</latency>
<timestamp>true</timestamp>
<success>true</success>
<label>true</label>
<code>true</code>
<message>true</message>
<threadName>true</threadName>
<dataType>true</dataType>
<encoding>false</encoding>
<assertions>true</assertions>
<subresults>true</subresults>
<responseData>false</responseData>
<samplerData>false</samplerData>
<xml>false</xml>
<fieldNames>true</fieldNames>
<responseHeaders>false</responseHeaders>
<requestHeaders>false</requestHeaders>
<responseDataOnError>false</responseDataOnError>
<saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
<assertionsResultsToSave>0</assertionsResultsToSave>
<bytes>true</bytes>
<sentBytes>true</sentBytes>
<url>true</url>
<threadCounts>true</threadCounts>
<idleTime>true</idleTime>
<connectTime>true</connectTime>
</value>
</objProp>
<stringProp name="filename"></stringProp>
</ResultCollector>
<hashTree/>
<DebugSampler guiclass="TestBeanGUI" testclass="DebugSampler" testname="Debug Sampler" enabled="true">
<boolProp name="displayJMeterProperties">false</boolProp>
<boolProp name="displayJMeterVariables">true</boolProp>
<boolProp name="displaySystemProperties">false</boolProp>
</DebugSampler>
<hashTree>
<BeanShellAssertion guiclass="BeanShellAssertionGui" testclass="BeanShellAssertion" testname="BeanShell Assertion" enabled="true">
<stringProp name="BeanShellAssertion.query"> import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
String csvFile =
"C:/Workspace/JmeterProjects/JMeterOutput_${__time(MM-dd-yyyy)}.csv";
BufferedReader bufferedReader = null;
String line = "";
String SEPARATOR = ",";
try {
bufferedReader = new BufferedReader(new FileReader(csvFile));
int counter = 1;
while ((line = bufferedReader.readLine()) != null)
{
String[] items = line.split(SEPARATOR);
print("--------------------LOGGING STARTS-----------------------");
for(int i = 0; i < items.length; i++)
{
log.info(items[i]);
}
print("--------------------LOGGING ENDS-------------------------");
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}</stringProp>
<stringProp name="BeanShellAssertion.filename"></stringProp>
<stringProp name="BeanShellAssertion.parameters"></stringProp>
<boolProp name="BeanShellAssertion.resetInterpreter">false</boolProp>
</BeanShellAssertion>
<hashTree/>
</hashTree>
</hashTree>
</hashTree>
</hashTree>
</jmeterTestPlan>
答案 1 :(得分:0)
相当于Groovy的东西是这样的:
sed -e '1,/pattern/{/pattern/ba};b;:a;r rfile' file
演示:
Don't inline JMeter Functions into scripts, use code-based equivalents instead