DaveH
DaveH

Reputation: 3

JMeter - Using cookie field as variable

have been looking into JMeter recently. I need to get an authentication string from a cookie and use it when posting a request to a different path. The Auth string changes each time the login page is hit.

Is there a way in JMeter to use one cookie for all paths in a test when the paths are different?

IE-

Path to Get Cookie: Webserver: someURL.net

Path: /some/login/path

Use the cookie value: Webserver: someURL.net

Path: /somewhere/different

I have set the below JMeter properties to be able to use Cookies as needed.

CookieManager.check.cookies=false
CookieManager.save.cookies=true
CookieManager.allow_variable_cookies=true

When I run the samplers the result for the request to /somewhere/different

Returns [no cookies]

I can see the cookie data present in the Request when the path is /some/login/path

I have tried defining a User-Defined cookie but I need to get the auth string first to use it in the different path.

When I do this I can see the cookie data added to the request to /somewhere/different but the var is not being set. I don't think this is the right way to solve the challenge.

To get the auth string first I tried to use the Controller "User defined Variables" to store the cookie value and pass it back to the Cookie Manager- this did not work.

And I looked at using the RegEx extractor to get the value so I could use it but I'm not sure that this can be used to get cookie values?

My understanding is that you cannot use more than one cookie manager per Thread group. As I type this I realize that the solution might be that I must use separate threads and pass the cookie value from one thread to another.

Apologies if the question is framed poorly first time here, signed up to ask after searches didn't turn up a solution. If you need more info/screens of my JMeter set up for any of the scenarios I tried above I'll add them. And thank you.

Upvotes: 0

Views: 2047

Answers (1)

Dmitri T
Dmitri T

Reputation: 168002

I'm afraid you won't be able to manipulate cookies using built-in JMeter features, you'll need to go deeper and invoke JMeter API methods from scripting test elements.

For instance:

  1. Add a Beanshell PostProcessor as a child of the first request
  2. Put the following code into the PostProcessor(s) "Script" area:

    import org.apache.jmeter.protocol.http.control.Cookie;
    import org.apache.jmeter.protocol.http.control.CookieManager;
    
    CookieManager manager = ctx.getCurrentSampler().getCookieManager();
    for (int i = 0; i < manager.getCookieCount(); i++) {
        Cookie cookie = manager.get(i);            
        if (cookie.getName().equals("your_cookie_name")) {
             Cookie newCookie = cookie;
             newCookie.setPath("/your/new/path");
             manager.remove(i);
             manager.add(newCookie);
             ctx.getCurrentSampler().setCookieManager(manager);
             break;
        }
    }
    
  3. Change your_cookie_name and /your/new/path as per your requirements

It will create a new one with the different path. JMeter's HTTP Cookie Manager doesn't allow 2 cookies with the same name so the old one has to be removed.

References:

Upvotes: 2

Related Questions