i have a collection which contains a login request with a test script which stores the token value in environment variable.
If i run the collection within Postman it works, i can see the token value.
If i run the collection with newman the token variable is never stored, it’s empty.
This a blocking for me i have a node js script where i run my collection with login then i export the environment into a new update environment json file.
If the updated environment file is empty i cannot continue the test.
my answers:
What scripts are you using within the Collection, to store the variable?
var jsonData = JSON.parse(responseBody)
pm.environment.set(“token”,jsonData.token);
What’s the Newman command you’re using?
newman run c:\test\login.postman_collection.json -e c:\test\token.postman_environment.json
You should be able to parse that file, and get the token (which I’m assuming is all you need from the login request) and then pass the token as a variable to the second collection run.
If you are using node.JS though, you might as well just request the token directly through the standard API modules. The code is similar to a sendRequest() in Postman.
What I can’t tell though is whether the current values are included in that environment export. Usually when you export an environment, it doesn’t include current values (for security reasons). If it doesn’t export the current values, then this isn’t going to work.
i tried with node.js but it doesn’t export the current values, then this isn’t going to work for me. i can only do some basic tests and that’s all.
Hi,
here is my answer:
You haven’t said what didn’t work:
newman did not save the token value in environment json file.
If you’re using node.js for Newman: yes
The flow that I was expecting is for you to call the authentication API directly using the native Node.Js API module of your choice.
You would also need to include a test library and assertion library if you want to mimic the functionality of Postman or Pytest.
The code snippet generator has several different Node.Js modules covered as a starter for 10.
This should return you your token, which you would pass to the Newman command line as a variable alongside the aforementioned environment file.
It won’t update the environment file as current values do not export.
Current values when used with Newman will only exist for the current Newman instance.
So you can’t call two different collections using Newman and expect the value to persists between the two runs, or be exported if you choose that option in the Newman command line.
It’s unusual for authentication for a request to be outside of that collection.
You would normally want all code and requests related to a piece of functionality to be in the same collection.
Postman is developing features in relation to the re-usability of scripts and requests which may make this easier in the future.
Hi,
the last point is correct:
“Postman is developing features in relation to the re-usability of scripts and requests which may make this easier in the future.”
Will continue to use Postman but for automation i will use python or Java for now.