如何在JMeter中的页面之间更改cookie值

时间:2014-09-22 16:23:58

标签: cookies jmeter

我知道如何使用BeanShell PreProcessor修改cookie: How to modify / add to Cookie in JMeter?

感谢PMD UBIK-INGENIERIE的答案!

现在,我的问题是:如何修改页面之间的cookie值?让我解释一下,我有一个叫做“回答”的cookie。对于第一页是空的,即,Answers ="" (空),然后在第二页取值Answers =" - , - , - , - , - , - &#34 ;;最后在第三页中它需要更长的值Answers =" - ,A,B, - ,C, - ..."

如何修改不同页面中的相同Cookie?我见过CookieManager API:http://jmeter.apache.org/api/org/apache/jmeter/protocol/http/control/CookieManager.html

但有人可以用一个例子解释一下吗?谢谢!

1 个答案:

答案 0 :(得分:2)

由于不能有2个具有相同名称的Cookie,CookieManager足够聪明,可以使用新值替换现有Cookie(请参阅removeMatchingCookies(c); // Can't have two matching cookies行)

所以

  1. 请求1:不需要,因为您无法发送具有空值的cookie
  2. 请求2和3:使用相同的代码添加Beanshell PreProcessor,如:

    import org.apache.jmeter.protocol.http.control.Cookie;
    import org.apache.jmeter.protocol.http.control.CookieManager;
    
    CookieManager manager = ctx.getCurrentSampler().getProperty("HTTPSampler.cookie_manager").getObjectValue();
    Cookie cookie = new Cookie("Answers", "**VALUE**", sampler.getDomain(), sampler.getPath(), false, System.currentTimeMillis());
    manager.add(cookie);
    sampler.setCookieManager(manager);
    

    请求2的**Value**-,-,-,-,-,-,而请求3为-,A,B,-,C,- ...

  3. 有关Beanshell脚本和Beanshell食谱类型的更多信息,请参阅How to use BeanShell: JMeter's favorite built-in component指南。