简体   繁体   English

在JSR223 Sampler Jmeter中添加前一个采样器的Cookie

[英]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) . 我正在尝试使用JSR223 Sampler(Jmeter)从JSR223 Sampler执行ajax调用,就像在并行请求中一样

I am able to get a response from ajax calls that don't need login auth cookie. 我能够从不需要登录身份验证cookie的ajax调用中获得响应。 However, not getting a response from ajax calls that need auth cookie generated by the login. 但是,没有得到需要通过登录生成的auth cookie的ajax调用的响应。

I have added login http call before JSR223 sampler but cookie is not getting passed in request. 我在JSR223采样器之前添加了登录http调用,但cookie未在请求中传递。 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 2017-11-28 10:44:51,195错误oajpjsJSR223Sampler:JSR223脚本中的问题JSR223 Sampler,消息:javax.script.ScriptException:java.lang.NullPointerException:无法在null对象上调用方法getCookieCount()javax.script.ScriptException:java .lang.NullPointerException:无法在null对象上调用方法getCookieCount()

The error you're getting indicates that you don't have HTTP Cookie Manager added/enabled. 您收到的错误表明您没有添加/启用HTTP Cookie管理器


As an alternative to Groovy scripting you can use "normal" JMeter HTTP Request samplers which naturally support cookies, cache, headers, authorization, etc. 作为Groovy脚本的替代方法,您可以使用“普通”JMeter HTTP请求采样器,它自然支持cookie,缓存,标头,授权等。

In order to be able to execute them in AJAX-like parallel manner put the samplers under Parallel Controller . 为了能够以类似AJAX的并行方式执行它们,将采样器置于并行控制器下 You can install the Parallel Controller using JMeter Plugins Manager 您可以使用JMeter插件管理器安装并行控制器

JMeter插件管理器并行控制器

Check that you added CookieManager in your test pkan. 检查您是否在测试pkan中添加了CookieManager。

Also use a JSR223 Pre Processor instead of a JsR223 Sampler. 也可以使用JSR223预处理器代替JsR223采样器。

As said before, do you have a HTTP cookie manager ? 如前所述,您有HTTP cookie管理器吗? And this is the code to add a cookie into the cookie manager 这是将cookie添加到cookie管理器的代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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