简体   繁体   English

如何在JMeter中修改/添加Cookie?

[英]How to modify / add to Cookie in JMeter?

I'm very new to JMeter and need your help on how to modify a cookie. 我是JMeter的新手,需要你的帮助才能修改cookie。

Here is the scenario: I'm testing an assessment/test taking website that offers multiple answers to questions. 这是一个场景:我正在测试一个评估/测试网站,提供多个问题答案。 When a user makes his selections and hits the submit button, the JavaScript in the page appends his answers (eg, "Answers = BBAACDA...") to the cookie and makes the next GET request (instead of a POST request!). 当用户进行选择并点击提交按钮时,页面中的JavaScript会将他的答案(例如,“Answers = BBAACDA ...”)附加到cookie并发出下一个GET请求(而不是POST请求!)。

Since, JMeter does not execute JavaScript (as popularly mentioned in its manual - it's not a browser), it cannot append the answers to the cookie. 因为,JMeter不执行JavaScript(如其手册中通常提到的 - 它不是浏览器),它不能将答案附加到cookie。 As a result, my test plan fails to recognize user interaction. 因此,我的测试计划无法识别用户交互。

How can I add/append/modify a dynamic cookie? 如何添加/添加/修改动态cookie? Thanks in advance! 提前致谢!

--Ishti --Ishti

Use a Beanshell pre-processor or better a Jsr223 Pre-Processor with groovy and use code mentionned here: 使用Beanshell预处理器或更好的带有groovy的Jsr223预处理器并使用此处提到的代码:

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);

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 遵循http://jmeter.apache.org/api/org/apache/jmeter/protocol/http/control/Cookie.html中的定义

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 . 可以使用与https://stackoverflow.com/a/38505077/5747304相同的方式在groovy预处理器脚本中手动修改或添加cookie。

Here's how to find and edit a cookie by browsing all cookies in the cookie manager: 以下是通过浏览cookie管理器中的所有cookie来查找和编辑cookie的方法:

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 . 以下是添加或删除的cookie管理器方法列表...: 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 以下是修改域名及其到期日期等更多属性的cookie方法列表...: 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. 应该知道,根据cookie管理器中选择的标准,经理仍然可以在请求之前修改手动修改的值,因此您必须小心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM