gregPi
gregPi

Reputation: 128

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?

Upvotes: 3

Views: 6116

Answers (4)

Ron Lu
Ron Lu

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

housten
housten

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

mkhurmi
mkhurmi

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

A.Joly
A.Joly

Reputation: 2387

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

Upvotes: 1

Related Questions