JMeter uses beanshell variable in HTTP request

I am an absolute rookie here (JAVA, I mean), spent hours searching for a solution, now I just want to shoot myself.
I want to create a string in the beanshell statement that is placed right above the HTTP request.

  • In beanshell, I wrote:

    String docid="abcd"; 

    (actually I want to combine the string with some variables)

  • In the HTTP request, send the parameters i add ${docid} .

+8
jmeter
source share
3 answers

In the BeanShell statement description section, you can find the following:

  vars - JMeterVariables - eg vars.get("VAR1"); vars.put("VAR2","value"); vars.putObject("OBJ1",new Object()); props - JMeterProperties (class java.util.Properties) - eg props.get("START.HMS"); props.put("PROP1","1234"); 

So, to set the jmeter variable in beanshell code (the BeanShell Assertion probe in your case), use the following:

 String docid = "abcd"; vars.put("docid",docid); 

or simply

 vars.put("docid","abcd"); 

and then you can forward it as $ {docid}, as you did in your HTTP request.

+14
source

If you do not know Java well, you can use any of the BSF or JSR223 test elements, and then select Javascript as the scripting language

http://jmeter.apache.org/usermanual/component_reference.html#JSR223_Sampler

+3
source

If you need to pass a value from one shell sampler bean to another, you must use variables.

 vars.put("a", "something") 

In another sampler, you should have something like:

 String otherSampler = vars.get("a") 

About Debugging Shell Samplers - It's Not That Easy. I suggest using the SampleResult object. How to use it, you can see here Debugging bean Shell Sampler

0
source

All Articles