2

I had been using Beanshell pre-processor to add custom cookies created through javascript. While it used to work perfectly earlier, with the same line of codes I started to recieve errors in my pre-processor.

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


WARN  - jmeter.modifiers.BeanShellPreProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval    Sourced file: inline evaluation of: ``import org.apache.jmeter.protocol.http.control.CookieManager; import org.apache. . . . '' : TargetError 

The lines of code is as per suggestion provided in https://javaworks.wordpress.com/2011/08/05/setting-cookie-in-jmeter/

import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.protocol.http.control.Cookie;

try{
CookieManager manager = sampler.getCookieManager();
Cookie cookie1 = new Cookie("cookie1","value","localhost","/",false,0);
manager.add(cookie1);

}catch (Throwable ex) {
log.error("Error in Beanshell", ex);
throw ex;
}

I came across several post in stackoverflow related to beanshell. However, none answers why imports in beanshell are failing. The error log:

018/07/02 16:24:47 ERROR - jmeter.util.BeanShellTestElement: Error in Beanshell java.lang.NullPointerException
at org.apache.jmeter.protocol.http.control.CookieManager.match(CookieManager.java:386)
at org.apache.jmeter.protocol.http.control.CookieManager.removeMatchingCookies(CookieManager.java:402)
at org.apache.jmeter.protocol.http.control.CookieManager.add(CookieManager.java:307)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at bsh.Reflect.invokeMethod(Reflect.java:134)
at bsh.Reflect.invokeObjectMethod(Reflect.java:80)
at bsh.Name.invokeMethod(Name.java:858)
at bsh.BSHMethodInvocation.eval(BSHMethodInvocation.java:75)
at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:102)
at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:47)
at bsh.BSHBlock.evalBlock(BSHBlock.java:130)
at bsh.BSHBlock.eval(BSHBlock.java:80)
at bsh.BSHBlock.eval(BSHBlock.java:46)
at bsh.BSHTryStatement.eval(BSHTryStatement.java:86)
at bsh.Interpreter.eval(Interpreter.java:645)
at bsh.Interpreter.eval(Interpreter.java:739)
at bsh.Interpreter.eval(Interpreter.java:728)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.jmeter.util.BeanShellInterpreter.bshInvoke(BeanShellInterpreter.java:163)
at org.apache.jmeter.util.BeanShellInterpreter.eval(BeanShellInterpreter.java:186)
at org.apache.jmeter.util.BeanShellTestElement.processFileOrScript(BeanShellTestElement.java:151)
at org.apache.jmeter.modifiers.BeanShellPreProcessor.process(BeanShellPreProcessor.java:59)
at org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:786)
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:445)
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:410)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:241)
at java.lang.Thread.run(Unknown Source)

1 Answers1

0
  1. First of all, there is no need in scripting, you can add custom cookies using HTTP Cookie Manager GUI

    JMeter add cookie via GUI

  2. If you still want to code try upgrading to the latest JMeter version

  3. Try playing with different "Policy" values in the HTTP Cookie Manager, i.e. try using standard or netscape
  4. Try adding the next line to user.properties file:

    CookieManager.check.cookies=false
    

    JMeter restart will be required to pick the property up

  5. In general it is recommended to switch to JSR223 Test Elements and Groovy language since JMeter 3.1 so consider migrating to JSR223 PreProcessor.
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Dmitri, the values of the cookies in my application needs to be updated frequently within a single loop whenever user navigates to new page, so I need to use preprocessor. however, even with JSR223 I get java null point error. Do I need any extra jar when I import CookieManager/Cookie in my preprocessor? – user2747610 Jul 04 '18 at 12:57
  • The point it to **have** the HTTP Cookie Manager **added** to your test plan and it has to be in the Sampler's [Scope](https://wiki.apache.org/jmeter/UserManual/ScopingRules). You should not need any extra jars as Cookie Manager is a part of JMeter HTTP protocol module and it should be already in your classpath. See [Modifying Cookies in JMeter with Groovy ](https://www.blazemeter.com/blog/modifying-cookies-in-jmeter-with-groovy) guide for more details on cookies manipulation from scripts if needed – Dmitri T Jul 04 '18 at 13:11