BeanShell Sampler (Beta Code)

This sampler allows you to write a sampler using the BeanShell scripting language.

Please note that the BeanShell jar file is not included with JMeter; it needs to be separately downloaded. For full details on using BeanShell please see the BeanShell web-site.

Parameters

Attribute

Description

Required

Name

Descriptive name for this controller that is shown in the tree.

No

Parameters

List of parameters to be passed to the script file or the script.

No

Script File

Name of a file to be used as a BeanShell script

Yes, unless Script filled in

Script

Script to be passed to BeanShell

Yes, unless Script File filled in

N.B. Each Sampler instance has its own BeanShell interpeter, and Samplers are only called from a single thread.

If the property "beanshell.sampler.init" is defined, it is passed to the Interpreter as the name of a sourced file. This can be used to define common methods and variables. There is a sample init file in the bin directory: BeanShellSampler.bshrc.

If a script file is supplied, that will be used, otherwise the script will be used.

Before invoking the script, some variables are set up in the BeanShell interpreter:

The contents of the Parameters field is put into the variable "Parameters". The string is also split into separate tokens using a single space as the separator, and the resulting list is stored in the String array bsh.args.

The full list of variables that are set up is as follows:

  • log - the Logger
  • Label - the Sampler label
  • FileName - the file name, if any
  • Parameters - text from the Parameters field
  • bsh.args - the parameters, split as described above
  • SampleResult - pointer to the current SampleResult
  • ResponseCode = 200
  • ResponseMessage = "OK"
  • IsSuccess = true

When the script completes, control is returned to the Sampler, and it copies the contents of the following script variables into the corresponding variables in the SampleResult:

  • ResponseCode - for example 200
  • ResponseMessage - for example "OK"
  • IsSuccess - true/false

The Sampler ResponseData is set from the return value of the script.

The SampleResult variable gives the script full access to all the fields and methods in the SampleResult. For example, the script has access to the methods setStopThread(boolean) and setStopTest(boolean). Here is a simple (not very useful!) example script:

if (bsh.args[0].equalsIgnoreCase("StopThread")) {
    log.info("Stop Thread detected!");
    SampleResult.StopThread(bsh.args[1]);
}
return "Data from sample with Label "+Label;

With the following code you can access the HttpRequest body and modify it:

sampler = ctx.getCurrentSampler ();
args = sampler.getProperty ("HTTPsampler.Arguments");
httpArg = args.getObjectValue ().getArguments ().get (0);
body = httpArg.getObjectValue ().getProperty ("Argument.value").getStringValue ();
  • No labels