Reputation: 59
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
Reputation: 167992
Put the following code into "Text to send" input
TCPSampler.port=${__BeanShell(ctx.getCurrentSampler().getPort();,port)}
Broken down it consists of:
ctx
- is the shortcut to the instance of JMeterContext class. See JavaDoc for all available methodsSo __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