Beanshell

Write values in CSV using JMeter

Hola Testers,

On my last post we were talking about read values from csv external data source. I thought this time we should talk about write values on csv. Imagine our jmeter script is creating some number of files in our web application, and you need to know the names of those files to use it later. In this kind of scenario, we can use csv writer to keep our files name for future use.

Add a Beanshell PostProcessor for the HTTP request which creating files
Right click on HTTP request > Add > Post Processors > Beanshell Postprocessors
Screen Shot 2016-04-10 at 8.25.09 AM.png
(You may need a regular expression extractor to HTTP Request for extract the file name.)

Write a write script to write the file name on csv
You can write a simple beanshell script to write file’s name on csv. A sample script like below,

generatedDocID = vars.get("DocID");
filepath =vars.get("FilePath");
f = new FileOutputStream(filepath, true);
p = new PrintStream(f);
this.interpreter.setOut(p);
print(generatedDocID);
f.close();

Where “DocID” is the variable we used in regular expression extractor and “FilePath” is the variable name for the csv filepath/name. That variable you can initialise in user defined variables. 🙂

Advertisements

Delete Files inside a Folder using Jmeter

Hola Testers,

After a long time I’m gonna tell you how to delete files inside of folder using Jmeter. Actually sometimes we need this trick while we are removing created test data.

Step 1:
Open JMeter. Obviously, since we are using JMeter. We need to open it first

Step2:
Add “User defined Variables” element
Right Click on the Test Plan > Add > Config Element > User Defined Variables
Screen Shot 2016-03-22 at 11.29.50 AM.png

Step 3:
Add user defined variable
Click on the “User Defined Variables” element
Click “Add” button
Give the folder path with a variable name.
Screen Shot 2016-03-22 at 11.33.23 AM

Step 4:
Add a Thread Group
Right click on the Test Plan > Add > Threads (Users) > Thread Group
Screen Shot 2016-03-22 at 11.35.10 AM.png

Step 5:
Add a request (I’m adding HTTP Request)
Right click on the Thread Group > Add > Sampler > HTTP Request
Screen Shot 2016-03-22 at 11.38.27 AM.png

Step 6:
Add a Beanshell PreProcessor for the Request
Right click on the HTTP Request > Add > Pre Processors > Beanshell PreProcessor
Screen Shot 2016-03-22 at 11.40.14 AM.png

Step 7:
Write a beanshell script to delete files inside of the folder (I’m deleting all the .csv files inside of “jmeterFolder” on my desktop)
Select Beanshell PreProcessor
Enter below script

import org.apache.commons.io.FileUtils;
List files = FileUtils.listFiles(new File("${folderPath}"), new String[]{"csv"}, true);
for (File file : files) {
file.delete();
}

${folderPath}: variable which you declare on “User Defined Variables”
Screen Shot 2016-03-22 at 11.47.19 AM.png

Step 8:
Save the script and Run.
jmac.jpg