JMeter - 验证使用了特定的 Cookie 值?
JMeter - Verify a Specific Cookie Value was Used?
所以在我的测试计划中,我在我的线程组中设置了一个 Cookie 管理器,它为 1 个 Cookie 设置了一个特定的 Cookie 值。我们称它为MYID。我正在尝试找出一种方法来验证是否使用了这个特定 Cookie 的值来完成这个 HTTP 请求,因为如果我将我的 MYID 设置为特定值 *( 这实际上会告诉要访问哪个 Web 服务器to),说 "Server1",但是 Server1 已关闭,不可用,等等...... HAProxy 应该改变然后将您发送到 Server2.
所以基本上我想尝试确保 Cookie MYID 等于 "Server1" 一路通过 HTTP 请求。
我正在尝试使用 BeanShell PostProcessor 在请求 运行 之后验证 Cookie 的值,但是当我尝试使用一些代码时,我在 PreProcessor 中设置了一个不同的测试计划中的 cookie我的我收到一条错误消息:
错误信息:
Typed variable declaration : Attempt to resolve method: getCookieManager() on undefined variable or class name: sampler
下面是我在另一个测试计划中从 BeanShell 预处理器稍微修改的代码...
代码:
import org.apache.jmeter.protocol.http.control.Cookie;
import org.apache.jmeter.protocol.http.control.CookieManager;
CookieManager manager = sampler.getCookieManager();
for (int i = 0; i < manager.getCookieCount(); i++) {
Cookie cookie = manager.get(i);
if (cookie.getName().equals("MYID")) {
if (cookie.getValue().equals("Server1")) {
log.info("OK: The Cookie contained the Correct Server Number...");
} else {
log.info("ERROR: The Cookie did NOT contain the Correct Server Number...");
}
break;
}
}
对于错误,我认为 "sampler" 对象不再可用,因为请求已经 运行,或者类似的东西,但我不确定...
或者,我是否应该使用另一个 JMeter 对象来代替 "BeanShell PostProcessor" 来验证 Cookie 的值是否正确..?
如有任何想法或建议,我们将不胜感激!
提前致谢,
马特
如果您尝试从 Beanshell 中的父采样器获取 cookie 管理器 PostProcessor - 您需要使用 ctx.getCurrentSampler(),而不是 "sampler",因为它没有暴露在脚本变量中。
所以只需更改此行:
CookieManager manager = sampler.getCookieManager();
到
CookieManager manager = ctx.getCurrentSampler().getCookieManager();
您的脚本应该会按预期开始工作。
ctx
是一个 shorthand 到 JMeterContext 的实例,getCurrentSampler()
方法名称是不言自明的。
有关 Beanshell 脚本的更多信息,请查看 How to use BeanShell: JMeter's favorite built-in component 指南。
所以在我的测试计划中,我在我的线程组中设置了一个 Cookie 管理器,它为 1 个 Cookie 设置了一个特定的 Cookie 值。我们称它为MYID。我正在尝试找出一种方法来验证是否使用了这个特定 Cookie 的值来完成这个 HTTP 请求,因为如果我将我的 MYID 设置为特定值 *( 这实际上会告诉要访问哪个 Web 服务器to),说 "Server1",但是 Server1 已关闭,不可用,等等...... HAProxy 应该改变然后将您发送到 Server2.
所以基本上我想尝试确保 Cookie MYID 等于 "Server1" 一路通过 HTTP 请求。
我正在尝试使用 BeanShell PostProcessor 在请求 运行 之后验证 Cookie 的值,但是当我尝试使用一些代码时,我在 PreProcessor 中设置了一个不同的测试计划中的 cookie我的我收到一条错误消息:
错误信息:
Typed variable declaration : Attempt to resolve method: getCookieManager() on undefined variable or class name: sampler
下面是我在另一个测试计划中从 BeanShell 预处理器稍微修改的代码...
代码:
import org.apache.jmeter.protocol.http.control.Cookie;
import org.apache.jmeter.protocol.http.control.CookieManager;
CookieManager manager = sampler.getCookieManager();
for (int i = 0; i < manager.getCookieCount(); i++) {
Cookie cookie = manager.get(i);
if (cookie.getName().equals("MYID")) {
if (cookie.getValue().equals("Server1")) {
log.info("OK: The Cookie contained the Correct Server Number...");
} else {
log.info("ERROR: The Cookie did NOT contain the Correct Server Number...");
}
break;
}
}
对于错误,我认为 "sampler" 对象不再可用,因为请求已经 运行,或者类似的东西,但我不确定...
或者,我是否应该使用另一个 JMeter 对象来代替 "BeanShell PostProcessor" 来验证 Cookie 的值是否正确..?
如有任何想法或建议,我们将不胜感激!
提前致谢,
马特
如果您尝试从 Beanshell 中的父采样器获取 cookie 管理器 PostProcessor - 您需要使用 ctx.getCurrentSampler(),而不是 "sampler",因为它没有暴露在脚本变量中。
所以只需更改此行:
CookieManager manager = sampler.getCookieManager();
到
CookieManager manager = ctx.getCurrentSampler().getCookieManager();
您的脚本应该会按预期开始工作。
ctx
是一个 shorthand 到 JMeterContext 的实例,getCurrentSampler()
方法名称是不言自明的。
有关 Beanshell 脚本的更多信息,请查看 How to use BeanShell: JMeter's favorite built-in component 指南。