BeanShell Scripting in JMeter

How to Use BeanShell in JMeter

Beanshell is one of the most advanced JMeter built-in components. It supports Java syntax and extends it with scripting features like loose types, commands, and method closures. If your test case is uncommon and implementation via embedded JMeter components becomes tricky or even impossible, BeanShell can be an excellent option to achieve your objectives.

BeanShell entities in JMeter have access to both internal JMeter APIs and any external classes that are loaded into the JMeter classpath (be sure to drop necessary jars into /lib/ext folder of your JMeter installation and place all necessary “import” statements at the top of your BeanShell scripts).

All JMeter versions provide the following BeanShell-enabled components:

  • Beanshell Sampler – A standalone sampler
  • Beanshell PreProcessor – A pre-processor to another sampler that is executed before the sampler and can be used for prerequisite setup (i.e. to generate some input). 
  • Beanshell PostProcessor –  A post-processor that is executed after the sampler and can be used for recovery or clean-up.
  • Beanshell Assertion – An advanced assertion with full access to JMeter API. Java conditional logic can be used to set the assertion result.
  • __Beanshell Function – A JMeter Function that allows execution of custom BeanShell code during a sampler run.

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 

${__property(NAME_VALUE)}

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");

log.info("************Decoded Data ="+Decoded_Data_Str);

vars.put("CsrfToken",decoded);

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.

Actual_str = vars.get ("ActualValue");
If (! Actual_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 postprocessor to write data in a CSV file.

Data_Str = vars.get("DataToWrite");
File_Path = vars.get("TestDataFilePath");
 // pass true if want to append to existing file  
 // if want to overwrite, then don't pass the second argument 
 FileWriter fstream = new FileWriter(File_Path, true);
 BufferedWriter out = new BufferedWriter(fstream);
 out.write(Data_Str);
 out.write(System.getProperty("line.separator"));
 out.close();
 fstream.close();

How to Check the existence of a CSV test data file:

Use below lines of code inside a preprocessor to check the existence of a CSV file.

FilePath = vars.get("TestDataFilePath");
File TestDataFile = new File(FilePath); 
if (!TestDataFile.exists()) 
{
      SampleResult.setSuccessful(false);
      SampleResult.setResponseMessage("Failed to find File");
      SampleResult.setResponseData("ERORR!! - FILE NOT FOUND: " + TestDataFile.getPath(),"UTF-8");
      log.error("*******ERORR!! - FILE NOT FOUND: " + TestDataFile.getPath());
      IsSuccess = false;
      SampleResult.setStopTestNow(true);
}
 else
 {
 	 SampleResult.setResponseData("FILE FOUND: " + TestDataFile.getPath(),"UTF-8");
    	 log.info("********FILE FOUND: " + TestDataFile.getPath());
}