BeanShell Scripting in JMeter

How to pass data from one thread group to other in Jmeter.

I want to pass the “UserName” variable value from ThreadGroup1 to ThreadGroup 2. Jmeter property will be used to accomplish this.

In Thread Group 1, Add a Bean shell Post Processor and put below code

import org.apache.jmeter.util.JMeterUtils;

JMeterUtils.setProperty("NAME_VALUE", vars.get("UserName"));

In Thread Group2 use below line of code 


How to convert URL encode data into TEXT?

By using bean shell post processor element in Jmeter we can convert by using below code.

String Encoded_Data_Str = vars.get("URLEncodedData");

String Decoded_Data_Str = URLDecoder.decode(Encoded_Data_Str, "UTF-8");"************Decoded Data ="+Decoded_Data_Str);


Custom Assertion in Jmeter – Bean Shell Assertion

Add a beanShell Assertion to request (Add->Assertions) and use below code to check the expected value with actual value fetched from the response of the request.

Expected_str = vars.get ("ActualValue");
If (! Expected_str.equals ("ExpectedValue")
Failure = true;
FaliureMessage = "Expected value is not matched with actual value";

How to write data stored in jmeter variable to a CSV file.

Use below lines of code inside a beanShell post processor to write the data into a CSV file.

FilePath = vars.get("C:\\Performance_Testing\\Test_Data\\Datafile.csv");
File TestDataFile = new File(FilePath); 
if (!TestDataFile.exists()) {
      SampleResult.setResponseMessage("Failed to find File");
      SampleResult.setResponseData("FILE NOT FOUND: " + TestDataFile.getPath(),"UTF-8");
      log.error("FILE NOT FOUND: " + TestDataFile.getPath());
 	 SampleResult.setResponseData("FILE FOUND: " + TestDataFile.getPath(),"UTF-8");"FILE FOUND: " + TestDataFile.getPath());