Patricia
Patricia

Reputation: 15

Empty csv file while using BeanShell Postprocessor

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:

Upvotes: 0

Views: 538

Answers (4)

Chris
Chris

Reputation: 1

#The below mentioned solution worked for me: 

OrderID = vars.get("primaryRefID");
FileWriter fstream = new 
FileWriter("C://Users/Documents/JMeter/OrderID.csv",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write("TC-"+OrderID);
out.write(System.getProperty("line.separator"));
out.close();
fstream.close();

Upvotes: 0

Dmitri T
Dmitri T

Reputation: 168122

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

Upvotes: 0

Patricia
Patricia

Reputation: 15

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

Upvotes: 0

Ori Marko
Ori Marko

Reputation: 58802

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

Upvotes: 0

Related Questions