3

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?

gregPi
  • 118
  • 1
  • 7

4 Answers4

2

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.

mkhurmi
  • 786
  • 4
  • 12
1

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

A.Joly
  • 2,317
  • 2
  • 20
  • 25
  • 1
    Yes, in Newman I can use variables from scope: global and environment, and I do it. But i'm trying to use variable from scope collection, but I don't know if it is possible. – gregPi Oct 25 '18 at 14:45
0

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

housten
  • 126
  • 9
0

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?

Ron Lu
  • 31
  • 2