newman icon indicating copy to clipboard operation
newman copied to clipboard

Sync updated environment variables during the Newman run

Open g7skim opened this issue 5 years ago • 41 comments

  1. Newman Version (can be found via newman -v): Newman 4.0.0
  2. OS details (type, version, and architecture): MacOS 10.13.4 (17E199)
  3. Are you using Newman as a library, or via the CLI? CLI

I use the Newman and Postman software to testing website.

My construction looks like:

newman run "https://api.getpostman.com/collections/<collection_id>?apikey=<my_postman_api_key>“ --environment "https://api.getpostman.com/environments/<environment_id>?apikey=<my_postman_api_key>" --export-environment "https://api.getpostman.com/environments/<environment_id>?apikey=<my_postman_api_key>" --insecure

all works fine but my environment values don't update after the Newman running.

In my request, I use the next Pre-request Script to update the value:

var mail = pm.environment.get("mail_randomizer"); pm.environment.set("mail_randomizer", Number(mail) + 1);

After sending this request in Postman the value "mail_randomizer" is gone up by 1. But after running the Newman it doesn't work.

How can I export the environment correctly in Newman?

g7skim avatar Aug 16 '18 13:08 g7skim

@g7skim --export-environment option accepts the path to the file where Newman will output the final environment variables file.

Newman doesn't update the changes made in the environment during the run yet. But this looks like a feature request, we need to figure out a better way to implement this.

codenirvana avatar Aug 17 '18 08:08 codenirvana

Any update facing same kind of issue

mianamirrashid avatar Sep 11 '18 22:09 mianamirrashid

Facing same issue where I need to update access token from Newman to run it from Jenkins as well, Postman no issue

bagrip avatar Sep 21 '18 05:09 bagrip

@mianamirrashid @bagrip can you confirm that your issue is related to the same issue discussed above?

For clarification, the issue is to sync the updates made to environment variables during the Newman run.

@g7skim I also updated the issue title to avoid confusion.

codenirvana avatar Sep 21 '18 07:09 codenirvana

@codenirvana yes my issue is linked to Newman not able to sync variables especially those which are being used in pre request. For me it is happening when I try to get response body in pre request and it is getting null while in postman it is getting response body.

mianamirrashid avatar Sep 21 '18 14:09 mianamirrashid

yes, newly generated token from Newman is not getting updated in Environment file and because of that next requests are failing because token is not updated and existing token expired. So Envrinment variables are not syncing up from Newman.

bagrip avatar Sep 21 '18 17:09 bagrip

@codenirvana Yes, the new issue title is more informative. Will this feature be added in some of the next releases?

g7skim avatar Sep 24 '18 16:09 g7skim

@g7skim I can't assure you an exact release date/version. 😅

But, we are working on a feature which will help to build sync features like this easily.

codenirvana avatar Sep 25 '18 12:09 codenirvana

@mianamirrashid @bagrip your issues are actually not related to this, it looks like environment variable is not being set or updated from the pre-request or test script.

Refer to scripts and variables documentation for more detail. This is most likely because of some issue in your script. If the issue still persists, feel free to open a new issue with a sample collection which can be used to reproduce that behavior.

codenirvana avatar Sep 25 '18 12:09 codenirvana

@codenirvana Actually same script and environment variable setup working fine in Postman but when I run it from Newman seeing issues where env variable (generated token) in environment file is not getting synced up.

bagrip avatar Sep 25 '18 22:09 bagrip

@bagrip You can export the final environment variables file before completing a run using the --export-environment <path> option.

In order to update the same input file, run:

$ newman run collection.json -e env.json --export-environment env.json

codenirvana avatar Sep 27 '18 08:09 codenirvana

I'm running into the same issue, My test also creates a global variable with a generated ID. I need that ID in the next test. It works fine in postman but it doesn't work in newman because the variables don't get filled during a run.

plebcity avatar Oct 04 '18 15:10 plebcity

@k0enf0rNL As mentioned above #1679 (comment), this issue is about syncing the updated environment variables with Postman Cloud.

codenirvana avatar Oct 05 '18 06:10 codenirvana

@codenirvana I'm wondering if this feature is now available on the last release, or not yet? :)

gmoussayan avatar May 09 '19 15:05 gmoussayan

is this feature available now. I am also facing the similar issue

richatyagi8 avatar May 16 '19 13:05 richatyagi8

I'm facing the same issue here with newman 3.6.0. this feature is crucial for my tests. Are there any workarounds?

kobo98 avatar Jun 04 '19 15:06 kobo98

@kobo98 there's a workaround - you can use PostMan API to update the environment: https://docs.api.getpostman.com/?_ga=2.106332483.1506077843.1559734342-1458974128.1553180662&version=latest. Add the request after all your tests.

The request body:

PUT https://api.getpostman.com/environments/{{environment_uid}}
{
    "environment": {
        "name": {{environmentName}},
        "values": [
        	{{jsonValues}}
        ]
    }
}

Put the following code in pre-request script to get current environment values:

pm.variables.set('environmentName', "\"" + pm.environment.name + "\"");

var variables = pm.environment.variables();

var jsonArray = Object.keys(variables).map(function(varname) {
    return "{\"key\": \"" + varname + "\", \"value\": \"" + variables[varname] + "\"}";
})

pm.variables.set('jsonValues', jsonArray.join(', '));

pfedotovsky avatar Jun 07 '19 16:06 pfedotovsky

I'm also facing this issue in newman version 3.9.3. My first test is a POST to our API The second test is a GET to our API to get the status. This GET needs the ID that is returned in the POST response. Works fine in Postman. We are not allowed to use the Postman cloud (company proxy is blocking this). Is there a way to vote for this feature request ? ;-)

EddyVos avatar Jun 25 '19 11:06 EddyVos

im also getting same issue with Version 4.5.1.

dilp avatar Jul 11 '19 10:07 dilp

I'm using v10.16.3 and I have the same issue. The thing is: I have a GET that gives me a url to download a file, so I have to download and generate a md5 and a json object to use in a subsequent request. So, I'm using newman inside a node script. I have searched a lot and was unable to find an example of API for newman to update an environment variable, so, I'm assuming that it's impossible in a simple API call way.

So, the workaround I made, based on "pfedotovsky" post, was: Using Postman: Put "postman.setEnvironmentVariable("<var_name>", "");" in "Pre-request Script" owned by the request that will need the md5 from the downloaded file. Export the collnection to use in the nodejs script.

In nodejs script: Listening to "beforeScript" of the item in question, I change the script "postman.setEnvironmentVariable("<var_name>", "");" to "postman.setEnvironmentVariable("<var_name>", "md5_value");". That way, the request is made using the required MD5 value.

I hope that this post can be useful for someone. And, if someone knows an easier way to do that, please, let's us know. It was very frustating to lose a couple of hours trying to figure out how to do it.

fabiano-bd avatar Oct 10 '19 12:10 fabiano-bd

This might be fixed with the latest version of newman - 4.5.6. Works for me now.

lucas937 avatar Nov 21 '19 07:11 lucas937

This might be fixed with the latest version of newman - 4.5.6. Works for me now.

When you say fixed, for clarity, you mean that now the feature is implemented? Again for clarity, you are running a newman execution that changes environment variables and those changes are then pushed to the cloud(synced)? If this is true, how exactly are you doing this? I doubt that the feature uses the same flag so there must be a new flag that exports the env object to the cloud and not to the loacl dev.

TaukSnarkyAgrud avatar Jan 21 '20 13:01 TaukSnarkyAgrud

Hey everyone, here's a small workaround bash script which solves this case until we implement it in newman. Might have to tweak this for your use-cases:

#!/usr/bin/env sh

## HELP:
##
## Ensure that `newman`, `jq` and `curl` are installed.
## 
## Set the following shell variables before execution:
## 
## - COLLECTION_ID - ID for the collection
## - ENVIRONMENT_ID - ID for the environment
## - POSTMAN_API_KEY - The Postman API key (from https://go.postman.co/integrations/services/pm_pro_api)
## - NEWMAN_OPTIONS (optional) - options for Newman

# sanity checks
if !hash newman 2>/dev/null; then
    echo  '`newman` not installed.'
    exit 1
fi
if !hash jq 2>/dev/null; then
    echo  '`jq` not installed. Needed for parsing JSON.'
    exit 1
fi
if !hash curl 2>/dev/null; then
    echo  '`curl` not installed. Needed for syncing with Postman.'
    exit 1
fi
if [[ -z $COLLECTION_ID ]]; then
    echo 'No collection ID. Set $COLLECTION_ID shell variable.'
    exit 1
fi
if [[ -z $ENVIRONMENT_ID ]]; then
    echo 'No environment ID. Set $ENVIRONMENT_ID shell variable.'
    exit 1
fi
if [[ -z $POSTMAN_API_KEY ]]; then
    echo 'No api key found. Set $POSTMAN_API_KEY shell variable.'
    exit 1
fi

# urls for collection and environment
collection_url="https://api.getpostman.com/collections/$COLLECTION_ID?apikey=$POSTMAN_API_KEY"
environment_url="https://api.getpostman.com/environments/$ENVIRONMENT_ID?apikey=$POSTMAN_API_KEY"

# a temporary file where we export the environment
env_temp_file=$(mktemp /tmp/newman-env.json.XXXXXX)

# run the command
newman run $collection_url --environment $environment_url --export-environment $env_temp_file $NEWMAN_OPTIONS

# convert the exported environment to payload for Postman API
env_payload=$(cat $env_temp_file | jq -r '{ "environment": { "values": [
    .values[] | {
        "key": .key,
        "value": .value|tostring,
        "type": .value|type
    }
] } }')

# sync environment
curl --location --request PUT \
    "$environment_url" \
    --header 'Content-Type: application/json' \
    --data-raw "$env_payload"

# cleanup
rm $env_temp_file

coditva avatar Jan 22 '20 09:01 coditva

related issue? https://github.com/postmanlabs/newman/issues/2192

ziazon avatar Mar 14 '20 16:03 ziazon

@codenirvana,

Team please fix this ASAP, also im using newman version 5.0.0 but still this above issue in this version... And this is very important point to fix on very high priority... opened this issue on Aug 16, 2018, but not give any solution on yet....approximately 21 months.....

Madhusudanpatidar avatar May 18 '20 13:05 Madhusudanpatidar

I agree.

This would be a very handy feature as it would allow much more interactive tests to be run in CI/CD scenarios where for example you want to follow a Customer Journey test before declaring the API as being ready for Production so to speak, in the current scenario you would need multiple collections if you want to the following.

Call API 1 to get token Call ${API2}/getCustomer with token value returned from API1 Call ${API2}/getCustomerHistory with value returned in previous requests response such as an accountId Call ${API3}/getTransactionHistory with value returned in previous requests response such as a financialAccountId

In the above you would need to split this into 4 collections and export the environment variable file after each run, this is cumbersome and not ideal IMO

Fez29 avatar Aug 30 '20 09:08 Fez29

Do we have a solution on this yet?

bhargavi1716 avatar Sep 18 '20 16:09 bhargavi1716

I agree.

This would be a very handy feature as it would allow much more interactive tests to be run in CI/CD scenarios where for example you want to follow a Customer Journey test before declaring the API as being ready for Production so to speak, in the current scenario you would need multiple collections if you want to the following.

Call API 1 to get token Call ${API2}/getCustomer with token value returned from API1 Call ${API2}/getCustomerHistory with value returned in previous requests response such as an accountId Call ${API3}/getTransactionHistory with value returned in previous requests response such as a financialAccountId

In the above you would need to split this into 4 collections and export the environment variable file after each run, this is cumbersome and not ideal IMO

This is also not working for me while running from newman

bhargavi1716 avatar Sep 18 '20 16:09 bhargavi1716

I agree. This would be a very handy feature as it would allow much more interactive tests to be run in CI/CD scenarios where for example you want to follow a Customer Journey test before declaring the API as being ready for Production so to speak, in the current scenario you would need multiple collections if you want to the following. Call API 1 to get token Call ${API2}/getCustomer with token value returned from API1 Call ${API2}/getCustomerHistory with value returned in previous requests response such as an accountId Call ${API3}/getTransactionHistory with value returned in previous requests response such as a financialAccountId In the above you would need to split this into 4 collections and export the environment variable file after each run, this is cumbersome and not ideal IMO

This is also not working for me while running from newman

shivanipremi avatar Oct 17 '20 14:10 shivanipremi

Does this issue got resolved ?

shivanipremi avatar Oct 17 '20 14:10 shivanipremi