【发布时间】:2018-04-03 14:58:53
【问题描述】:
我们可以将 beanshell 断言变量(在线程中)传递给 jmeter 中的 cookie 管理器用户定义变量(线程外)吗?
This is the image of user defined cookies which are been used on my request
【问题讨论】:
标签: jmeter session-cookies assertions beanshell
我们可以将 beanshell 断言变量(在线程中)传递给 jmeter 中的 cookie 管理器用户定义变量(线程外)吗?
This is the image of user defined cookies which are been used on my request
【问题讨论】:
标签: jmeter session-cookies assertions beanshell
我不认为它会这样工作,你可以使用类似的东西:
在“init”线程组中:
import org.apache.jmeter.protocol.http.control.CookieManager;
CookieManager manager = ctx.getCurrentSampler().getProperty("HTTPSampler.cookie_manager").getObjectValue();
props.put("cookiecount",String.valueOf(manager.getCookieCount()));
for (int i=0;i<manager.getCookieCount();i++){
// code to convert Cookie information to JMeter Properties
props.put("cookie_name_" + i, manager.get(i).getName());
//…..
}
在“动作”线程组中:
import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.protocol.http.control.Cookie;
import org.apache.jmeter.testelement.property.JMeterProperty;
CookieManager manager = ctx.getCurrentSampler().getProperty("HTTPSampler.cookie_manager").getObjectValue();
int count = Integer.parseInt(props.getProperty("cookiecount"));
for (int i=0;i<count;i++) {
Cookie cookie = new Cookie(props.getProperty("cookie_name"+i),props.getProperty("cookie_value"+i), props.getProperty("cookie_domain"+i),props.getProperty("cookie_path"+i), Boolean.parseBoolean(props.getProperty("cookie_secure"+i)), Long.parseLong(props.getProperty("cookie_expires"+i)));
manager.add(cookie);
}
JMeterProperty cookieprop = ctx.getCurrentSampler().getProperty("HTTPSampler.cookie_manager");
cookieprop.setObjectValue(manager);
ctx.getCurrentSampler().setProperty(myprop);
最好使用 Thread Groups 本地的 2 个(或 3 个)单独的而不是全局 HTTP Cookie 管理器。
【讨论】: