Newman: Provide an option to use an existing cookie store in Newman

Created on 4 Aug 2015  路  24Comments  路  Source: postmanlabs/newman

Before starting I clear the postman context (no lingering cookies)
I read the cookie value using javascript in my test
I post the value to the environment variable
postman.setEnvironmentVariable('chtrsessionid',postman.getResponseCookie("chtrsessionid").value);
... value is correct in environment variable as displayed in postman ...
add a header to the next test in sequence ..

Cookie chtrsessionid={{chrtrsessionid}}

I run the subsequent test .. .. (Jetpacks test).
Works fine in postman.

Export the test collection ... and run the test in Newman .. fails
Dump of environment shows the value has not been written to the environment

After reviewing the newman code...
Would I have to add/integrate the functionality of the interceptor logic in chrome/postman to Newman?
The cookies ... without postman jetpacks ... interceptor seem to always return null?
Is their a way to log the response(data, headers, cookies) in newman?
I can modify the source ...?
Is that the intention?
George

feature request pending-close v3

Most helpful comment

I encountered the same problem, i think this is very important feature. Can anyone help put it in the high prioritize

All 24 comments

Newman does not work with the Interceptor. To add cookies through Newman, you'll have to set the Cookie header like you do for the second request.

In newman, you can use console.log() in your test script. You have access to the response data, headers - see https://www.getpostman.com/docs/jetpacks_sandbox

Is it possible to use newman to read the cookies from a request? I am trying to read them, but I think you cant, as you need to enable the Interceptor https://www.getpostman.com/docs/jetpacks_sandbox and thats only possible with postman :( so i cant integrate login based on cookies with newman.

The interceptor is needed only for the Chrome app.

If you export a collection which has a "Cookie" header in its request[s], Newman should send the request with the cookie. If you receive a response where the server wants the client to set cookies, you can use postman.getResponseCookie("Set-Cookie") to get the header.

There's no persistent cookie store like there is in Postman(with the Interceptor).

If i use this test in the chrome app it successful.
console.log("getting cookie")
console.log(responseCookies[0])
tests["there cookies"] = responseCookies[0] != null;

While running on Newman i get:
getting cookie
EXCEPTION - ReferenceError: evalmachine.:2
console.log(responseCookies[0])
^
responseCookies is not defined

responseCookies is only defined in Postman - https://www.getpostman.com/docs/jetpacks_sandbox

There is no cookie store in Newman. You can only capture cookies if the server sends a 'Set-Cookie' header in the response.

Yes, so the point is that Newman cant read cookies, only headers. Maybe i can add some JS script in the pre request section to get the cookie. Any other idea?

Nope, not as of now.

Since the requests are delegated to the request library, I don't see this being too difficult to implement:

Right where the request lib is imported:
https://github.com/postmanlabs/newman/blob/develop/src/runners/RequestRunner.js#L2

We could add something like:

if (process.env.USE_COOKIE_JAR === true){
   requestLib = requestLib.defaults({jar: true});
}

any new about this issue?

I would just like to stay updated on the issue that this function is not supported via newman.

postman.getResponseCookie is not a function 200 137ms https://...

Please let me know if this will be addressed or if i need to work on a different platform to handle these requests.

any other options to achieve this?

I would suggest that the newman should support the same functionality as postman does. Testing authentication and especially cookies is quite important.
On the other hand, if newman acts exactly like postman we could prevent managing split collections for each "application"

@czardoz - can we have a cookie store for newman provided as a file maybe? TBD

I encountered the same problem, i think this is very important feature. Can anyone help put it in the high prioritize

postman.getResponseHeader("Set-Cookie") returns undefined when server has sent multiple "Set-Cookie" header. Therefore, I'm essentially blocked from using newman on our CI

Which version of Newman are you on? @robosung

@shamasis I'm using newman through docker (postman/newman_ubuntu1404) so it's version 2.1.2. It turned out the real issue was that 302 status code that the server was returning. Adding "-R" resolved this issue. Thank you @czardoz and @siddhant for the help.

@elssar - this then means time to bump up Newman docker! 馃槣

Hey @shamasis,

I see you closed this issue. Since last e-mail about Newman v3 and other Postman updates, Im running the new newman npm in a Windows environment and still receiving the same error as above.

I read multiple threads about this and the whole documentation. I have the feeling I cannot use:
postman.getResponseHeader("ActualCookieName") in my Newman. But should I write extra JS code to handle when being run by newman? And if yes, then call: postman.getResponseHeader("Set-Cookie") ?

CC: @abhijitkane

Oh damn. This got closed because we deviated from context and I lost track - oh dear. Apologies.

No worries :-)

@shamasis is it an idea to implement what suggested:

rclayton-the-terrible commented on Oct 30, 2015
Since the requests are delegated to the request library, I don't see this being too difficult to implement:

Right where the request lib is imported:
https://github.com/postmanlabs/newman/blob/develop/src/runners/RequestRunner.js#L2

We could add something like:

if (process.env.USE_COOKIE_JAR === true){
requestLib = requestLib.defaults({jar: true});
}

@calebrepkes have a look at the above PR.

Step 1: we are enabling the jar
Step 2: Allow a way to provide and export a cookie jar as a file to Newman.

Im sorry I didnt see it that quickly. Thnx.

Closing as the issue has been fixed on our end.

Was this page helpful?
0 / 5 - 0 ratings