1
votes

i'm trying to save my variables into a csv file by using BeanShell Postprocessor, Code:

String id = "${userID}";
FileWriter fstream = new FileWriter("JmeterBean.csv",true);
fstream.write(id+"\n");
fstream.close();

Test Plan:

  1. HTTP Request GetUsersById => return all IDs

  2. Json extractor => from my response

    {"@class":"com.test.dto.userDTO",
          "author":"John",
          "id":"89BC331D723F",  },
    
    
    {"@class":"com.test.dto.userDTO",
          "author":"Alex",
          "id":"FTH7JBDRF567",
       }
    
    • Name of variale : userID
    • JSON path expression: $.[?(@.@class=='com.test.dto.userDTO')].id
    • Match Numbers: -1
  3. BeanShell Postprocessor

But my csv file is always empty and look like that:

3
probably you didn't set userID, show how you define/set the variableuser7294900
I add a Debug Sampler and it displays : 'userID_1=89BC331D723F' and 'userID_2=FTH7JBDRF567'Patricia
so use userID_1user7294900
But in this case it will write only the first userIDPatricia
follow this link to find solution (stackoverflow.com/questions/22364953/…)Patricia

3 Answers

1
votes
String id  = vars.get("userID");
FileWriter fstream = new FileWriter("JmeterBean.csv",true);
fstream.write(id+"\n");
fstream.close();

I did it but i got the same result with null in my csv file:

CSV file

0
votes

Use vars to get variable

String id = vars.get("userID");

vars - (JMeterVariables) - gives read/write access to variables: vars.get(key);

And prefer using JSR223 PostProcessor over Beanshell PostProcessor

0
votes
  1. If the JSON Extractor produces more than 1 match you don't have the userID variable, you will have something like:

    userID_1=89BC331D723F
    userID_2=FTH7JBDRF567
    userID_matchNr=2
    

    so I would recommend double checking which JMeter Variables are produced by the JSON Extractor using Debug Sampler and View Results Tree listener combination.

  2. Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting

Assuming all above add JSR223 PostProcessor (make sure it's located after the JSON Extractor) and use the following code:

1.upto(vars.get('userID_matchNr') as int, { number ->
    new File('JmeterBean.csv') << vars.get('userID_' + number) << System.getProperty('line.separator')
})