12

I'm an absolute rookie here (JAVA i mean), spent hours looking for a solution, now i just want to shoot myself.
I want to create a string in the beanshell assertion which is placed right above the HTTP Request.

  • In the beanshell i wrote:

    String docid="abcd";
    

    (in actuality i wish to concatenate a string with some variables)

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

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
Suyash
  • 625
  • 1
  • 5
  • 22

3 Answers3

24

In BeanShell Assertion description section you can find the following:

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

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

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

or simply

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

and then you can refer it as ${docid}, as you've done in your HTTP Request.

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
  • Thank you so very much for your help!! If i'm not asking too much how will i be able to concatenate a variable in the beanshell assertion only. my objective is actually to run a loop and keep concatenating the string. I was planning to run this sampler in a loop controller and concatenate the variable to itself with little modifications with each loop run – Suyash Jul 27 '12 at 07:32
  • Ignore the above comment, i managed to do concatenation and also looping, but somehow i am not able to pass the value from one beanshell sampler to another. i tried the vars.get("varname") but its not working. it only works for http request type variables. – Suyash Jul 27 '12 at 08:34
  • Hm. Try use [Debug Sampler](http://jmeter.apache.org/usermanual/component_reference.html#Debug_Sampler) at least to track problem variable between beanshell samplers. As well you can also use props.get/.put in the same manner - maybe this will work better. – Aliaksandr Belik Jul 27 '12 at 14:07
3

If you don't know Java well, you can use any of BSF or JSR223 Test elements and then select Javascript language as scripting language

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

UBIK LOAD PACK
  • 33,980
  • 5
  • 71
  • 116
2

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

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

In other sampler, you should have something like:

String otherSampler = vars.get("a")

About debugging Shell Samplers - It is not so easy. I suggest to use SampleResult object. How to use it, you can see here Debugging Bean Shell Sampler

Fenton
  • 241,084
  • 71
  • 387
  • 401