Reputation: 128
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?
Upvotes: 3
Views: 6116
Reputation: 41
I face the similar issue, but because I need to override the collection variable from command line using --env-var(there is no --collection-var in options) but seems not working, I think because collection variable is narrow than the environment so won't work.
I think your case works because you have different variable name, is it?
Upvotes: 0
Reputation: 136
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
Upvotes: 1
Reputation: 816
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.
Upvotes: 2