Michael S Daniel
Michael S Daniel

Reputation: 59

Store Jmeter sampler properties value using beanshell preprocessor

I've a test plan with TCP sampler with HOST and PORT defined using TCP sampler config.

I have defined the 'port' value for the TCP connection defined in TCP sampler config and I need this value (TCPSampler.port=3001) to be part of request to the server.

I am trying to use beanshell pre-processor to capture and store it on a user defined variable.

Any idea about how to achieve this.

Advanced thanks for the help.

Upvotes: 0

Views: 1442

Answers (1)

Dmitri T
Dmitri T

Reputation: 167992

Put the following code into "Text to send" input

TCPSampler.port=${__BeanShell(ctx.getCurrentSampler().getPort();,port)}

Broken down it consists of:

  1. __Beanshell function which allows to execute arbitrary beanshell code in any place of the script
  2. ctx - is the shortcut to the instance of JMeterContext class. See JavaDoc for all available methods
  3. getCurrentSampler() - is aforementioned JMeterContext class method which provides access to the instance of the current sampler
  4. getPort is the method of TCPSampler class as in your case it will be TCP sampler

So __Beanshell function executes the script and saves result into port variable which can be later accessed anywhere in current thread group.

For Beanshell PreProcessor (if you still want to use it) relevant code will look like:

int port = sampler.getPort();
vars.put("port", String.valueOf(port));

See How to use BeanShell: JMeter's favorite built-in component guide for comprehensive information on Beanshell scripting in Apache JMeter. `

Upvotes: 0

Related Questions