Ishtiaque Hussain
Ishtiaque Hussain

Reputation: 383

How to change cookie values between pages in JMeter

I know how to modify cookies using BeanShell PreProcessor from: How to modify / add to Cookie in JMeter?

Thanks to PMD UBIK-INGENIERIE for the answer!

Now, my question is: how do I modify the cookie value between pages? Let me explain, I've a cookie called 'Answers' that for the first page is empty, i.e., Answers="" (empty), then in second page takes the value Answers="-,-,-,-,-,-"; finally in a third page it takes a longer value Answers="-,A,B,-,C,- ..."

How do I modify the same cookie in different pages? I have seen the CookieManager API: http://jmeter.apache.org/api/org/apache/jmeter/protocol/http/control/CookieManager.html

But can anyone please explain with an example? Thank you!

Upvotes: 2

Views: 2004

Answers (1)

Dmitri T
Dmitri T

Reputation: 168147

As there cannot be 2 cookies having the same name, the CookieManager is smart enough to replace existing cookie with a new value (see removeMatchingCookies(c); // Can't have two matching cookies line)

So

  1. Request 1: nothing required as you cannot send a cookie having empty value
  2. Requests 2 and 3: Add a Beanshell PreProcessor with the same code like:

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

    Where **Value** for Request 2 will be -,-,-,-,-,- and for Request 3 will be -,A,B,-,C,- ...

For more information on Beanshell scripting and kind of Beanshell cookbook refer to How to use BeanShell: JMeter's favorite built-in component guide.

Upvotes: 2

Related Questions