newman
newman copied to clipboard
Sync updated environment variables during the Newman run
- Newman Version (can be found via
newman -v
): Newman 4.0.0 - OS details (type, version, and architecture): MacOS 10.13.4 (17E199)
- 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 --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.
Any update facing same kind of issue
Facing same issue where I need to update access token from Newman to run it from Jenkins as well, Postman no issue
@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 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.
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.
@codenirvana Yes, the new issue title is more informative. Will this feature be added in some of the next releases?
@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.
@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 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 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
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.
@k0enf0rNL As mentioned above #1679 (comment), this issue is about syncing the updated environment variables with Postman Cloud.
@codenirvana I'm wondering if this feature is now available on the last release, or not yet? :)
is this feature available now. I am also facing the similar issue
I'm facing the same issue here with newman 3.6.0. this feature is crucial for my tests. Are there any workarounds?
@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(', '));
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 ? ;-)
im also getting same issue with Version 4.5.1.
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 nodejs script:
Listening to "beforeScript" of the item in question, I change the script "postman.setEnvironmentVariable("<var_name>", "
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.
This might be fixed with the latest version of newman - 4.5.6. Works for me now.
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.
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
related issue? https://github.com/postmanlabs/newman/issues/2192
@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.....
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
Do we have a solution on this yet?
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
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
Does this issue got resolved ?