'Can I use postman collection variable inside newman?

Using variables from scope: collection inside Postman works fine.

But when I export collection and use it inside Newman it does not work as I expected.

1) Variabes are inside collection json, in the end of file - ok.

2) I use this code:

var obj = {};
obj.categories = pm.variables.get("category_id");
obj.packages = pm.variables.get("package_id"); 
obj.type = "add";
pm.globals.set("switch_json", JSON.stringify(obj));
console.log("request body: " + pm.globals.get("switch_json"));

in pre-request script to get value of 2 collection variables (category_id, package_id).

3) Inside Postman all works fine, console.log return:

request body: {"categories":"14","packages":"2","type":"add"}

4) Inside Newman console.log return only:

'request body: {"type":"add"}'

Does it mean Newman do not support collection variables?



Solution 1:[1]

Collection Variables are stored in the variables tab in collections under Edit. Initial values are shared when you export a collection and not the current values. Newman will access these(initial Values) values.

Solution 2:[2]

you shall save your environment (ie. my_environment.json) then, in your newman command use the -e option to use it.

have a look here for newman options

hope this helps

Alexandre

Solution 3:[3]

I looked around and around for a way to set a collection variable on the command line, without using an environment file, without finding any answers so I just tested using --env-var and it worked.

In the case of the original question it would look like this:

newman run xxxxxxx --env-var package_id=14

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mkhurmi
Solution 2 A.Joly
Solution 3 housten