0

I am trying to execute the following Beanshell script in JMeter and it throws an error in the log. The script is:

import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.protocol.http.control.Cookie;
CookieManager manager = sampler.getCookieManager();
Cookie cookie = new Cookie("ApiSession",props.get("MyCookie"),"","/",false,0);
manager.add(cookie);

The error in the log file is:

jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import org.apache.jmeter.protocol.http.control.CookieManager; import org.apache. . . . ''

It is not happy with the line: manager.add(cookie);

If I comment it out, then the script runs, but obviously doesn't do what I want. So, not sure what the problem is.

It is not helpful that I can't see whole of the debug information. Jmeter log records only part of the actual error message (as above) and that message is cut in the middle. Switching on debugging mode doesn't help.

DOgl
  • 53
  • 2
  • 9
  • I dont see how `sampler.getCookieManager()` would even work. In a BeanShell sampler, that variable is not one of the defined accessible variables. You would have to define `sampler` as a variable like this , for it to work: `HTTPSampler sampler = ctx.getCurrentSampler()`. – djangofan Mar 19 '15 at 00:06
  • @djangofan, when I do this I get the error about missing HTTPSampler: *Typed variable declaration : Class: HTTPSampler not found in namespace* – Shai Alon Jan 17 '17 at 15:36
  • Well, this post was from a long time ago. Not sure if things are the same in Jmeter 3.1+. – djangofan Jan 18 '17 at 04:31

1 Answers1

1

If you want to see full error message you'll need to surround problematic statement in try/catch block and print stacktrace to sdtout / log.

Particularly sharing cookies between thread groups use case is highlighted in How to use BeanShell guide.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thanks for the tip on try/catch. Will try it out. With the sharing cookie example, I will try it as well. It seems that they define the CookieManager from the HTTP Cookie Manager, which is slightly different to the way I've done it. Will let know if that has worked. – DOgl Jan 20 '14 at 10:27
  • If you don't have HTTP Cookie Manager enabled than I'm not surprised to know that you having errors. By the way if you set `CookieManager.save.cookies=true` property in jmeter.properties or in command line as `jmeter -JCookieManager.save.cookies=true` you'll be able to access cookies as generic JMeter variables – Dmitri T Jan 20 '14 at 14:27
  • Yes, that worked! Adding HTTP Cookie Manager to the Test plan fixed it. I thought that since I am importing the class definition in BeanShell script that would be enough to make it work, but apparently not. Thanks a lot. – DOgl Jan 22 '14 at 15:52
  • @DmitriT, i know it is a similar question, but could you please help me on http://stackoverflow.com/questions/39919693/how-to-pass-complete-cookies-from-one-thread-group-to-another – Vishal Puliani Oct 12 '16 at 13:43