TYale
TYale

Reputation: 3

JMETER: Not able to add Cookie to request

First time poster, long time reader - For one of my Performance tests, a cookie is not being passed for one of the requests. I have been trying many of the suggestions that I found here and other places online and I still cant seem to manually add a cookie to any request.

So what I am trying to do now is just follow the sample from Blazemeter and even with my basic setup, I still can't get the cookie added. I have to be missing something easy and I am running out of hair to pull out.

(I can't post pics yet, so image link is below)

Jmeter version - 5.0 r1840935

User.Properties Updated -

CookieManager.check.cookies=false
CookieManager.save.cookies=true

I have the HTTP Cookie Manager at the ThreadGroup Level. The cookie policy is set to standard, but I also tried to use Default with no difference. The HTTP Request has following settings: serverName: blazedemo.com, Method:GET, Path:password/reset

In the JSR223 Preprocessor (Groovy 2.4.15 / Groovy Scripting 2.0) under the HTTP request, my code looks like the following (with some debug statements)

 import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.protocol.http.control.Cookie;

//Get cookie manager
CookieManager cm = ctx.getCurrentSampler().getCookieManager()
Cookie cookie = new Cookie("sampleCookie", "sample", "opencart.abstracta.us", "/Mytest/done", false, 1557578515)
cm.add(cookie)
Cookie c=cm.get(0)
log.info("*********Cookie #3:" + c.getName() + "cookie value: " + c.getValue())
log.info("******************** Cookies count: " + cm.getCookieCount())

The Jmeter debug log shows the following so it appears that the cookie is successfully added to the store and I think it should work.

2020-01-22 19:10:35,936 DEBUG o.a.j.p.h.c.CookieManager: **Add cookie to store** opencart.abstracta.us  TRUE    /Mytest/done    FALSE   1557578515  sampleCookie    sample
2020-01-22 19:10:35,936 INFO o.a.j.m.JSR223PreProcessor: *********Cookie #3:sampleCookiecookie value: sample
2020-01-22 19:10:35,936 INFO o.a.j.m.JSR223PreProcessor: ******************** Cookies count: 1

However, the log also shows that there were no cookies found for the request -

2020-01-22 19:10:35,938 DEBUG o.a.j.p.h.c.HC4CookieHandler: Found 0 cookies for http://blazedemo.com/password/reset

When looking at the Request Body, I see the following:

GET http://blazedemo.com/password/reset
GET data:
[no cookies]

I don't see a syntax error, but do I have something setup incorrectly? If I add a cookie to the Cookie Manager in the 'User-Definied Cookies' that cookie is indeed passed along to the request.

TestPlan Image

Upvotes: 0

Views: 3207

Answers (2)

Dmitri T
Dmitri T

Reputation: 168002

  1. You're adding the cookie for domain opencart.abstracta.us and path /Mytest/done
  2. You're hitting the domain blazedemo.com and path /password/reset
  3. You're expecting to see the cookie in the request - this will not happen

According to RFC 6265 this behaviour is absolutely expected, if you want to see the programmatically generated cookie - it's domain and path must match the domain/path in the HTTP Request sampler

So change this line:

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

to this one:

Cookie cookie = new Cookie("sampleCookie", "sample", "blazedemo.com", "/password/reset", false, 1557578515)

and your scenario should start working as expected.

enter image description here

References:

Upvotes: 1

naveen rai
naveen rai

Reputation: 1

HTTP Cookie Manager, stores and send the cookies just as a browser does, so it will be better first to validate the request/flow manually in browser's Developers tools, whether any cookies are going there or not.

Because Cookies are messages that web servers pass to your web browser in an earlier response header [e.g. set-cookies: ] and the same value used by the browser to send in next request.

In the same way, the HTTP cookies manager is also working.

So, you need to first verify whether you are getting any cookies from the server or not.

If not, Jmeter will not send it back.

with Regards, Naveen Rai

Upvotes: 0

Related Questions