How to specify globals directly using newman.run in nodejs

How can I specify global variables directly using the newman nodejs module?

In the newman CLI, I am able to pass in global variables using the --global-var param:

--global-var someVariable="someValue" --global-var someOtherVariable="someOtherValue"

I can’t seem to do this using the newman nodejs package. I would have thought this would work:

newman.run({
        collection: constants.postmanCollectionUrl + "?apikey=" + constants.postmanApiKey,
        noColor: true,
        globals: {
            user: "someUser",
            password: "somePassword"
        }
    })

But it does not. How can I do this?

Figured it out…

I needed to create a globals option in the following format:

newman.run({
        collection: constants.postmanCollectionUrl + "?apikey=" + constants.postmanApiKey,
        noColor: true,
globals:
            {
                "id": "683ad527-9279-f62f-ff8d-663df9026ca8",
                "name": "Postman Globals",
                "values": [
                    {
                        "key": "user",
                        "value": "someUser",
                        "enabled": true,
                        "type": "text"
                    },
                    {
                        "key": "password",
                        "value": "somePassword",
                        "enabled": true,
                        "type": "text"
                    }
                ],
                "_postman_variable_scope": "globals"
            }
    })

I would suggest that the documentation on github be updated to make this more clear.

2 Likes

Totally agree. Any help as PR would be great. @kunagpal, @_pjoshi_126, @deepakpathania789 - feel free to jump in.

@analytix.developer While the expected variable option format is documented here: https://github.com/postmanlabs/newman#newmanrunevents, your use case is valid from a usability perspective. In the future, Newman could be adjusted to accommodate the first format as well.

Once you know the format, either is okay. So, it would appear to me as good to stick to the usual structure of global variables. Best around be to put this up as an example and/or documentation.

1 Like