How to modify / add to Cookie in JMeter?

后端 未结 3 1804
深忆病人
深忆病人 2021-01-14 00:47

I\'m very new to JMeter and need your help on how to modify a cookie.

Here is the scenario: I\'m testing an assessment/test taking website that offers multiple answe

相关标签:
3条回答
  • 2021-01-14 01:27

    It is possible to modify or add a cookie manually in a groovy pre-processor script in the same way as https://stackoverflow.com/a/38505077/5747304.

    Here's how to find and edit a cookie by browsing all cookies in the cookie manager:

    import org.apache.jmeter.protocol.http.control.CookieManager;  
    import org.apache.jmeter.protocol.http.control.Cookie;
    
    log.info("#########################################################################");
    // cookie manager
    CookieManager manager = ctx.getCurrentSampler().getCookieManager();
    
    def NbOfCookies = manager.getCookieCount();
    
    for (def i = 0; i < NbOfCookies; i++) {
        log.info("Cookie n° " + (i+1) + ": " + manager.get(i).getName() + ": " + manager.get(i).getValue());
        if (manager.get(i).getName() == "Cookie_name_to_find") {
            log.info("MAJ of Cookie_name_to_find");
            manager.get(i).setValue("New_cookie_value");
            log.info("-> " + manager.get(i).getName() + ": " + manager.get(i).getValue());
        }
    }
    
    log.info("#########################################################################");
    

    Here is the list of cookie manager methods like add or delete ...: http://jmeter.apache.org/api/org/apache/jmeter/protocol/http/control/CookieManager.html.

    Here is the list of cookie methods to modify more properties like the domain, its expiration date ...: http://jmeter.apache.org/api/org/apache/jmeter/protocol/http/control/Cookie .html

    It should be known that according to the standart chosen in the cookie manager, the manually modified values can still be modified by the manager before the request so you have to be careful.

    0 讨论(0)
  • 2021-01-14 01:31

    Use a Beanshell pre-processor or better a Jsr223 Pre-Processor with groovy and use code mentionned here:

    • http://javaworks.wordpress.com/2011/08/05/setting-cookie-in-jmeter/

    Code:

    import org.apache.jmeter.protocol.http.control.CookieManager;  
    import org.apache.jmeter.protocol.http.control.Cookie;
    CookieManager manager = sampler.getCookieManager();
    Cookie cookie = new Cookie("<NAME>","<VALUE>","<HOST>","/",false,0);
    manager.add(cookie);
    
    0 讨论(0)
  • 2021-01-14 01:42

    I had to implement some changes in the code that worked for me:

    import org.apache.jmeter.protocol.http.control.CookieManager;  
    import org.apache.jmeter.protocol.http.control.Cookie;
    
    CookieManager manager = ctx.getCurrentSampler().getCookieManager();
    Cookie cookie = new Cookie("<NAME>","<VALUE>","<DOMAIN>","<PATH>",false,0, true, true, 0);
    manager.add(cookie);
    

    Following the definition in http://jmeter.apache.org/api/org/apache/jmeter/protocol/http/control/Cookie.html

    0 讨论(0)
提交回复
热议问题