本文介绍了JMeter - 验证使用了特定的Cookie值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 所以在我的测试计划中,我有一个Cookie管理器设置在我的线程组中设置一个Cookie的特定Cookie值为1 Cookie。让我们称为 MYID 。我试图找出一种方法来验证这个特定的Cookie的值被用来完成这一个HTTP请求,因为如果我将我的MYID设置为一个特定的值*(实际上告诉哪个web服务器去) ,但服务器已关闭,无法使用等.HAProxy应该更改此设置并将您发送到 Server2 。 我尝试使用BeanShell后处理程序来验证Cookie的值后请求被运行,但是当我尝试使用一些代码我有一个PreProcessor设置一个cookie在不同的测试计划我的我得到一个错误说: 错误讯息: 键入的变量声明:尝试解析方法:getCookieManager :sampler 下面是在另一个测试计划中从BeanShell预处理器稍微修改的代码... CODE: 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 Cookie cookie = manager.get(i); if(cookie.getName()。equals(MYID)){ if(cookie.getValue()。equals(Server1)){ log.info :Cookie包含正确的服务器编号...); } else { log.info(ERROR:Cookie does not contain the Correct Server Number ...); } break; } } 对于错误,我在想sampler对象不再可用,因为请求已经运行,或者沿着这些行,但我不知道... 或者,有另一个JMeter对象应该使用代替BeanShell PostProcessor来验证Cookie的值是否正确。 任何想法或建议都将非常感谢! 感谢提前, Matt 解决方案尝试从Beanshell PostProcessor 中的父采样器获取Cookie管理器 - 您需要使用 ctx.getCurrentSampler(),而不是sampler,因为它未在脚本变量中公开。 所以只需更改这一行: CookieManager manager = sampler。 getCookieManager(); 到 CookieManager manager = ctx.getCurrentSampler()。getCookieManager(); 您的脚本应该按照预期开始工作。 ctx 是 JMeterContext 实例和 getCurrentSampler()方法名是不言自明的。 有关Beanshell脚本的更多信息,请查看如何使用BeanShell:JMeter最喜欢的内置组件指南。 So in my Test Plan I have a Cookie Manager setup inside my Thread Group which sets a specific Cookie value for 1 Cookie. Let's call it, MYID. I'm trying to figure out a way to verify that this specific Cookie's value was used to complete this one HTTP Request, because if I set my MYID to a specific value *(which actually tells which web server to go to), say to "Server1", but Server1 is down, unavailable, etc... HAProxy should change this and send you to Server2.So basically I want to try and make sure that Cookie MYID was equal to "Server1" all the way through the HTTP Request.I am trying to use a BeanShell PostProcessor to verify the Cookie's value after the request is ran, but when I tried using some code I have inside a PreProcessor that sets a cookie in a different Test Plan of mine I get an error saying:Error Message: Typed variable declaration : Attempt to resolve method: getCookieManager() on undefined variable or class name: samplerAnd below here is the Code slightly modified from a BeanShell PreProcessor in another Test Plan I have...CODE: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; }}For the error, I was thinking the "sampler" object was no longer available since the Request was already run, or something along those lines, but I'm not sure...Or, is there another JMeter object I should be using instead of the "BeanShell PostProcessor" in order to verify the Cookie's value was correct..?Any thoughts or suggestion would be greatly appreciated!Thanks in Advance,Matt 解决方案 If you trying to get cookie manager from the parent sampler in the Beanshell PostProcessor - you need to use ctx.getCurrentSampler(), not "sampler" as it is not exposed in script variables. So just change this line:CookieManager manager = sampler.getCookieManager();to CookieManager manager = ctx.getCurrentSampler().getCookieManager();And your script should start working as you expect. ctx is a shorthand to JMeterContext instance and getCurrentSampler() method name is self-explanatory. For more information on Beanshell scripting check out How to use BeanShell: JMeter's favorite built-in component guide. 这篇关于JMeter - 验证使用了特定的Cookie值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-22 06:01