Tuesday, July 2, 2013

Jmeter Dynamic value Storing to a File using BSF sampler?

How can we save the dynamic values to a file?

We will achieve this by using two ways
1. bean shell scripting using BSF sampler
2. Using jp@gc - Dummy Sampler with jp@gc - Flexible File Writer

Example: For demonstration i am using greatandhra website. In this website i am correlating the id. Please see below how i am correlating this.


In Jmeter i am writing the regular expression extractor to correlate this value. For this i am doing following in the action in Jmeter

Step 1: Start Jmeter using Jmeter.bat file and add the "Thread group" to test plan.
Step2: Add the "Http Request "Sampler from "Samplers", Please see below for these steps









Step 3:Add the Greatandhra URL to the "Http sampler" as shown below






















Step 4: Add the Regular Expression Extractor as a child to "Http Request" sampler.[Right click on Http Request sampler --> Post Processors --> Regular Expression Extractor]. Please see the regular expression and tree view below



















Step 5:In this step I am using "BSF sampler" to write the correlated vale to the file. Below is the code

output = vars.get("id");// we need to pass the "Reference Name" of "Regular expression extractor"
//vars.get() function will get the Jmeter variable
log.info(output); //
f = new FileOutputStream("C:\\OfficeWork\\Java\\id.txt");
p = new PrintStream(f);
this.interpreter.setOut(p);
print(output);

Please see the BSF sampler below





















Finally verify the id.txt file, it should display the id.

No comments: