0
votes

I am trying to read a CSV file using Beah Shell Assertion . below is my bean shell script

       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();
                    }
               }

        }

But i have got the error saying

ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import java.io.BufferedReader; import java.io.FileNotFoundException; import java . . . '' Token Parsing Error: Lexical error at line 6, column 22. Encountered: "W" (87), after : "\"C:\"

I am not sure what could be error behind this

2

2 Answers

0
votes
  1. Don't use Beanshell for scripting, since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language
  2. Don't inline JMeter Functions into scripts, use code-based equivalents instead

Groovy equivalent would be something like:

new File('C:\Workspace\JmeterProjects/JMeterOutput_' + new Date().format('MM-dd-yyyy') + '.csv').readLines().each { line ->
    line.split(',').each { entry ->
        log.info(entry)
    }
}

Demo:

enter image description here

More information: Scripting JMeter Assertions in Groovy - A Tutorial

0
votes

The updated code that you shared is running fine with the exact same conditions including the file path on my windows system. Only file contents are different. Please find below the output. If you are still seeing the original error with the updated code, Please share more information like logs etc.

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 = 
      &quot;C:/Workspace/JmeterProjects/JMeterOutput_${__time(MM-dd-yyyy)}.csv&quot;;
       BufferedReader bufferedReader = null;
       String line = &quot;&quot;;
       String SEPARATOR = &quot;,&quot;;

     try {
         bufferedReader = new BufferedReader(new FileReader(csvFile));

              int counter = 1;
       while ((line = bufferedReader.readLine()) != null) 
          {
               String[] items = line.split(SEPARATOR);
        print(&quot;--------------------LOGGING STARTS-----------------------&quot;);
   for(int i = 0; i &lt; items.length; i++)
       {        
           log.info(items[i]);
       }   
        print(&quot;--------------------LOGGING ENDS-------------------------&quot;);
       }

       } 
       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>