jmeter - second post request is not using the JSESSIONID created on my log in post request

回眸只為那壹抹淺笑 提交于 2019-12-05 21:27:36

UPDATE!!!

Haven't noticed your https protocol.

All you need is to set either CookieManager.save.cookies=true property in your jmeter.properties file or add it as an argument to JMeter command line as

jmeter -JCookieManager.save.cookies=true -n -t path_to_jmx_script.jmx -l path_to_log_file.jtl

Leaving the rest of my response just in case anyone else will need it as a guide on how to share JMeter Cookies across different Thread Groups.

I'm not able to reproduce your use case in my environment using following scenarios:

  • HTTP Cookie Manager lives under Test Plan (same level as Thread Group(s))
  • HTTP Cookie Manager lives under Thread Group (same level as Samplers)

It's only reproducible if HTTP Cookie Manager added as a child of Login request. If it's your case - move it up 1 level to broaden it's scope.

If for some reason it doesn't help - see below for possible workaround details.

Your response code 204 doesn't sound like an error to me. I guess that the server would rather respond with something like 401 or 403 if there were problems with cookie-based authentication.

If you explicitly need to set cookie it still can be done via i.e. Beanshell

You need to do the following:

  1. If you're going to share cookies between different thread groups or need them as JMeter variables for any other reason set CookieManager.save.cookies=true property either in jmeter.properties file or specify it during JMeter startup as jmeter -JCookieManager.save.cookies=true
  2. Add Beanshell Post Processor to your Login Request with following code:

    import org.apache.jmeter.protocol.http.control.CookieManager;
    
    CookieManager manager = ctx.getCurrentSampler().getProperty("HTTPSampler.cookie_manager").getObjectValue();
    
    props.put("cookiecount", String.valueOf(manager.getCookieCount()));
    
    for (int i = 0; i < manager.getCookieCount(); i++) {
        props.put("cookie_name" + i, manager.get(i).getName());
        props.put("cookie_value" + i, manager.get(i).getValue());
        props.put("cookie_domain" + i, manager.get(i).getDomain());
        props.put("cookie_path" + i, manager.get(i).getPath());
        props.put("cookie_expires" + i, String.valueOf(manager.get(i).getExpires()));
        props.put("cookie_secure" + i, String.valueOf(manager.get(i).getSecure()));
    }
    
  3. Add Beanshell Pre Processor to your POST request with following code:

    import org.apache.jmeter.protocol.http.control.CookieManager;
    import org.apache.jmeter.protocol.http.control.Cookie;
    import org.apache.jmeter.testelement.property.JMeterProperty;
    
    
    CookieManager manager = ctx.getCurrentSampler().getProperty("HTTPSampler.cookie_manager").getObjectValue();
    
    int count = Integer.parseInt(props.getProperty("cookiecount"));
    
    for (int i = 0; i < count; i++) {
        Cookie cookie = new Cookie(props.getProperty("cookie_name" + i), props.getProperty("cookie_value" + i),
                props.getProperty("cookie_domain" + i), props.getProperty("cookie_path" + i),
                Boolean.parseBoolean(props.getProperty("cookie_secure" + i)),
                Long.parseLong(props.getProperty("cookie_expires" + i)));
        manager.add(cookie);
    }
    
    JMeterProperty cookieprop = ctx.getCurrentSampler().getProperty("HTTPSampler.cookie_manager");
    
    cookieprop.setObjectValue(manager);
    
    ctx.getCurrentSampler().setProperty(cookieprop);
    

Explanation:

The code at point 2 fetches all available cookies from HTTP Cookie Manager and stores them to JMeter Properties prefixed with cookie_

The code at point 3 reads all properties prefixed with cookie_, constructs JMeter Cookies from them and adds them to HTTP Cookie Manager.

See How to use BeanShell guide for more information on extending JMeter via scripting.

I had to keep the same Server Name/IP as my Log in POST and change my Path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!