Search code examples
jmeterjmeter-pluginsbeanshell

How can I access the cookie manager in beanshell?


I have seen many examples but none seem to work.

This is what I have setup:

enter image description here

I'm using a foreach to loop through my variables and have Cookie Manager use them: enter image description here

I want the beanshell to clear the cookies in the cookie manager on each loop so they get re-added from my vars, but I can't seem to access it.

I tried the following:

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

manager = sampler.getCookieManager();

That gives me this error:

Attempt to resolve method: getCookieManager() on undefined variable or class name: sampler

I tried doing this:

CookieManager cManager = ctx.getCurrentSampler().getCookieManager();

But that gives me this error:

Typed variable declaration : Error in method invocation: Method getCookieManager() not found in class'org.apache.jmeter.protocol.java.sampler.BeanShellSampler'

Edit: So as per a suggested solution I tried this:

enter image description here

And then this:

enter image description here

But that gives me this error:

2017/10/13 12:26:31 ERROR - jmeter.extractor.JSR223PostProcessor: Problem in JSR223 script JSR223 PostProcessor javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method clear() on null object

Solution

  • Don't use Beanshell because of Performance and it will be removed in future versions.

    • Add a JSR223 PreProcessor on the first HTTP Request
    • Select Groovy
    • Check "Cache compiled script if available"
    • Add following code:

    import org.apache.jmeter.protocol.http.control.CookieManager; import org.apache.jmeter.protocol.http.control.Cookie; CookieManager cm = sampler.getCookieManager(); cm.clear();

    This is what it should look like

    enter image description here