简体   繁体   中英

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 oajpjsJSR223Sampler: 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

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插件管理器并行控制器

Check that you added CookieManager in your test pkan.

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM