ajay
ajay

Reputation: 225

Adding Cookie from previous sampler in JSR223 Sampler Jmeter

I am trying to execute ajax calls from JSR223 Sampler like in parallel request using JSR223 Sampler (Jmeter).

I am able to get a response from ajax calls that don't need login auth cookie. However, not getting a response from ajax calls that need auth cookie generated by the login.

I have added login http call before JSR223 sampler but cookie is not getting passed in request. Tried by adding code:

HTTPSamplerProxy previousSampler = ctx.getPreviousSampler();
CookieManager cookieManager = previousSampler.getCookieManager();
HTTPSampleResult previousResult = (HTTPSampleResult)ctx.getPreviousResult();
log.info("Cookie Count is : "+ cookieManager.getCookieCount());

It throws below exception:

2017-11-28 10:44:51,195 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method getCookieCount() on null object javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method getCookieCount() on null object

Upvotes: 1

Views: 1941

Answers (3)

jeffrey Dorey
jeffrey Dorey

Reputation: 1

As said before, do you have a HTTP cookie manager ? And this is the code to add a cookie into the cookie manager

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

try {
    String params = vars.get("getCookieValue");
    ctx.getCurrentSampler().getCookieManager().add(new Cookie("COOKIENAME", params, "domain", "/url", true, Long.MAX_VALUE));
}
catch (Throwable ex) {
    log.error("Error in Beanshell", ex);
    throw ex;
}

Upvotes: 0

Dmitri T
Dmitri T

Reputation: 168147

The error you're getting indicates that you don't have HTTP Cookie Manager added/enabled.


As an alternative to Groovy scripting you can use "normal" JMeter HTTP Request samplers which naturally support cookies, cache, headers, authorization, etc.

In order to be able to execute them in AJAX-like parallel manner put the samplers under Parallel Controller. You can install the Parallel Controller using JMeter Plugins Manager

JMeter Plugins Manager Parallel Controller

Upvotes: 3

UBIK LOAD PACK
UBIK LOAD PACK

Reputation: 34566

Check that you added CookieManager in your test pkan.

Also use a JSR223 Pre Processor instead of a JsR223 Sampler.

Upvotes: 0

Related Questions