Newman does not run the script from a collection

Hi,

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.

Any help please thank you
Koubiac

Postman cannot save variable value. we have to send data to local server and connect to the local server, answer from chatgpt.

Hey @ps198478320 :wave:,

Can you provide more context and examples to the questions, can you visually show the workflow.

What scripts are you using within the Collection, to store the variable?

What’s the Newman command you’re using?

Are you using the --export-environment <path> flag to store the variables after the run has completed.

Without more information, it’s difficult to know exactly what you’re doing.

Hi Danny,

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

ok so we can do nothing with newman then.

If you are running a node.js script, then use the node version of Newman.

newman - npm (npmjs.com)

You should be able to export the environment once the run is completed.

node.js - how to export environment variable using newman API - Stack Overflow

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.

If you click on the code snippet options to the right of a request, you should get example code for your request in a variety of programming language.

There are a few options for Node.JS in there.

Use one of those methods to retrieve your token, when you can then pass to the Newman command line.

For example, the following is using the NodeJs Native API.

var https = require('follow-redirects').https;
var fs = require('fs');

var options = {
  'method': 'POST',
  'hostname': 'postman-echo.com',
  'path': '/post?test=123',
  'headers': {
    'Cookie': 'sails.sid=s%3A-TFdz3QlEQ4zA1Ua6EjFEw7ef3ZPxSD4.cEDPVqK4PzPqXFpEM39oGoCMUoLf3L%2FEkVZIAMrzPFE'
  },
  'maxRedirects': 20
};

var req = https.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function (chunk) {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });

  res.on("error", function (error) {
    console.error(error);
  });
});

req.end();

yes i saw the code snipets and i tried, it did not work.
but i achieved the goal with pytest.
Will come bakc to Postman later.
Thank you

Ok fair enough. Glad you’ve got a working solution.

You haven’t said what didn’t work, as you can certainly call API’s with a native node.js API client.

If you’re using node.js for Newman, it was just making sense to use a node.js library for this element as well instead of Python.

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

Ok, I’m not quite following the logic.

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.