Open g7skim opened 6 years ago
@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("
In nodejs script:
Listening to "beforeScript" of the item in question, I change the script _"postman.setEnvironmentVariable("
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 ?
@coditva Hello may I ask how's that implementation is going for this feature? It is really important for automated API testing...
It can be achieved with the following:
Ensure that environment values are updated to their desired values using postman.setEnvironmentVariable("var_name", "var_value");, or postman.setGlobalVariable in your collection request test scripts, where applicable.
To update the environment / globals file, you can use --export-environment / --export-globals, like so:
newman run coll.json -e env.json -g globals.json --export-environment env.json --export-globals globals.json
is this going to be fixed without using any hacks or workarounds? just curious as its been lodged 2018 and its 2021 now and probably those who initially raised it just gave up and move to other tools.
Did this get resolved with any Newman release? I need to set the environment variables at run time to be used in my next request within or outside the collection. This is really important for automation testing and CI/CD. Any updates are appreciated.
Incase, any one is still wondering on this, using collection variables instead of environment variables works with Newman
Any update on the above issue, is it fixed ..?
Any update? Is there any way that could update the initial value?
Well, we just managed workaround that the server always send unique ID, so you can always send a unique request... Because we could not w8 that long for the fix ^^
After wasting the whole afternoon I found out that pm.cookies doesn't work in newman but if it works pm.response.headers.get('set-cookie').split('=')[1]
I found the solution by making the asynchronous code synchronous: ` const newman = require("newman"); const fs = require("fs").promises;
const collections = [ "./IndividualCollection/SignIn.postman_collection.json", "./IndividualCollection/WorkSpace.postman_collection.json",
];
(async () => { let environmentData = JSON.parse( await fs.readFile( "./FeatureTesting_Variables.postman_environment.json", "utf8" ) );
for (const collection of collections) { const summary = await runCollection(collection, environmentData); if (summary) { environmentData = updateEnvironmentData(environmentData, summary); } }
await fs.writeFile( "./FeatureTesting_Variables.postman_environment.json", JSON.stringify(environmentData, null, 2) ); console.log("All collections executed and environment file updated."); })();
function runCollection(collection, environmentData) {
return new Promise((resolve, reject) => {
newman.run(
{
collection: collection,
environment: environmentData,
reporters: ["cli", "htmlextra", "json"],
reporter: {},
color: "auto",
},
(err, summary) => {
if (err) {
console.error(Error running collection: ${err}
);
reject(err);
} else {
resolve(summary);
}
}
);
});
}
function updateEnvironmentData(environmentData, summary) { const collectionEnvironment = summary.environment.values; environmentData.values = collectionEnvironment.map((variable) => ({ key: variable.key, value: variable.value, enabled: true, })); console.log( "Environment variables updated for collection:", summary.collection.name ); return environmentData; }`
Good day, I would like to add my voice to the feature request. It would really power up our API automation if we were able to do this in a more intuitive way. Many of us have spend long hours trying to make the awesomeness that can happen on Postman replicate in Newman. Please keep us posted on any update, thanks in advance :)
newman -v
): Newman 4.0.0I 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?