Search code examples
jmeterbeanshellcookiemanager

JMeter - Strange Behavior when Variables used in CookieManager


JMeter Version 2.13 r1665067

So I seem to be having some trouble when using User-Defined Variables and/or JMeter Property Variables inside a CookieManager.

If I use a variable as the value for a Cookie inside the CookieManager, whether or not it's a User-Defined Var or a Property Var I have the same problem when trying to view the Cookie's value(s) inside a BeanShell Pre and PostProcessor.

If my Cookie Manager has this below: *The 1st line is when using properties variable and 2nd line is when using a var from a User Defined Variable, *FYI both lines are NOT used at the same time:

CookieManager:

      NAME        VALUE                DOMAIN        PATH
1st)  MYID    ${__P(propCookie)}  www.mydomain.com     /
                      OR
2nd)  MYID     ${userCookie}      www.mydomain.com     /

The propCookie variable is passed on the CLI or defined in a .properties file like below:

COMMAND-LINE --> -JpropCookie=SRV1
IN PROP FILE --> propCookie=SRV1

And the userCookie variable is defined inside a User Defined Variables Config Element like so:

  NAME             VALUE           DESCRIPTION
userCookie   ${__P(propCookie)}   User var using prop variable as value


Then, when I run my test I can see in the Request Tab of the Results Tree that it is showing the Cookie and it has the correct value assigned to it, which is good... But when I attempt to view the Cookies in a BeanShell Pre/Post-Processor, it simply shows the Variable and NOT the actual value.

BeanShell PreProcessor Code

import org.apache.jmeter.protocol.http.control.Cookie;
import org.apache.jmeter.protocol.http.control.CookieManager;

log.info("### Inside BeanShell PreProcessor:");
CookieManager manager = sampler.getCookieManager();
log.info("Cookie Count = '" + manager.getCookieCount() + "'");

for (int i = 0; i < manager.getCookieCount(); i++) {
    Cookie cookie = manager.get(i);
    log.info("\t\t  Cookie.getName() = '" + cookie.getName() + "'");
    log.info("\t\t Cookie.getValue() = '" + cookie.getValue() + "'");
}

Here is the BeanShell Script's Output in the Log:

2015/04/29 14:33:00 INFO  - jmeter.util.BeanShellTestElement: ### Inside BeanShell PreProcessor: 
2015/04/29 14:45:58 INFO  - jmeter.util.BeanShellTestElement: Cookie Count = '1' 
2015/04/29 14:33:00 INFO  - jmeter.util.BeanShellTestElement:         Cookie.getName() = 'MYID' 
2015/04/29 14:33:00 INFO  - jmeter.util.BeanShellTestElement:        Cookie.getValue() = '${userCookie}' 


So as you can see in the output from the BeanShell, the getValue() function is printing the Variable assigned to the Cookie's value exactly like this --> "${userCookie}", and NOT what the actual Cookie's value is, which is "SRV1"...

Lastly, if I try to use the "COOKIE_" variable which is supposed to get automatically created, and I use this in BeanShell, vars.get("COOKIE_MYID"), it prints "null" every time... I have all the proper Cookie properties set in the jmeter.properties file, like these here, so I'm not sure what the problem is:

CookieManager.allow_variable_cookies=true
CookieManager.save.cookies=true
CookieManager.name.prefix=COOKIE_

I'm pretty much stumped for why this is happening so if anyone has any ideas for why this is happening, please feel free it would be greatly appreciated!

Thanks in Advance,
Matt


Solution

  • HTTP Request Sampler actually sets the cookie using CookieManager's getCookieHeaderForURL method which substitutes the value properly.

    In Beanshell,

    import org.apache.jmeter.protocol.http.control.Cookie;
    import org.apache.jmeter.protocol.http.control.CookieManager;
    
    manager = sampler.getCookieManager();
    log.info(manager.getCookieHeaderForURL(new URL("http://www.google.com")));   //update the URL
    

    This gives the cookies with updated values.


    CookieManager's get & add methods are used to get & add cookies to the HTTP Cookie Manager at run time. So, getValue method gives the value as it is. getCookieHeaderForURL gets the appropriate cookies from the Cookie Manager for the domain with updated values.