JMeter JSR223 PostProcessor GET COOKIE

痴心易碎 提交于 2019-12-11 07:39:06

问题


Every link I've found about cookies in JMeter is based on cookie manager. But unfortunately, JMeter losts cookies if path is changed, and I can't change JMeter properties to use solutions about that, so I have to use setting variable by JSR223 PostProcessor after request.

But I can't find any information about that - every link is about using HTTP Cookie Manager.

Please help with solution WITHOUT Cookie Manager.


回答1:


blazemeter has a good examples about creating cookies in JSR223

import org.apache.jmeter.protocol.http.control.*    
//Get cookie manager
CookieManager cm = sampler.getCookieManager()    
//Set cookie currency to EUR
cm.addFile("C:/work/jmeter-scripts-samples/cookie.txt")

The method CookieManager.addFile(String cookieFile) adds the cookie defined in the cookieFile parameter (cookie.txt in this example) to the cookie manager.

We will create a cookie by adding the following line to the PreProcessor script:

Cookie c = new Cookie("sampleCookie", "sample", "opencart.abstracta.us", "/", false, 1557578515)



回答2:


First of all described situation sounds very weird, JMeter should be able to parse the cookies given:

  1. The cookies are valid (fully compliant with RFC 6265)
  2. HTTP Request sampler has proper configuration (domain matches cookie domain, path matches cookie path, etc.)

If you're absolutely sure that your application is doing what it is supposed to be doing (including setting invalid cookies) you can work this around by making HTTP Cookie Manager less restrictive by:

  1. Adding the next line to user.properties file:

    CookieManager.check.cookies=false
    
  2. Using another Cookie Policy, i.e. netscape

As you can see even given Domain and Path are blank JMeter still adds the cookie to the request:

More information: HTTP Cookie Manager Advanced Usage - A Guide


In any case you should be able to extract cookies from the Set-Cookie header using Regular Expression Extractor and add it to the requests as it is described above.



来源:https://stackoverflow.com/questions/58910036/jmeter-jsr223-postprocessor-get-cookie

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