How to modify / add to Cookie in JMeter?

后端 未结 3 1823
深忆病人
深忆病人 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.

提交回复
热议问题