postmanlabs / newman

Newman is a command-line collection runner for Postman
https://www.postman.com
Apache License 2.0
6.88k stars 1.17k forks source link

Sync updated environment variables during the Newman run #1679

Open g7skim opened 6 years ago

g7skim commented 6 years ago
  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?

codenirvana commented 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.

mianamirrashid commented 6 years ago

Any update facing same kind of issue

bagrip commented 6 years ago

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

codenirvana commented 6 years ago

@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.

mianamirrashid commented 6 years ago

@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.

bagrip commented 6 years ago

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.

g7skim commented 6 years ago

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

codenirvana commented 6 years ago

@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 commented 6 years ago

@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.

bagrip commented 6 years ago

@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.

codenirvana commented 6 years ago

@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
plebcity commented 6 years ago

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.

codenirvana commented 6 years ago

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

gmoussayan commented 5 years ago

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

richatyagi8 commented 5 years ago

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

kobo98 commented 5 years ago

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

pfedotovsky commented 5 years ago

@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(', '));
EddyVos commented 5 years ago

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 ? ;-)

dilp commented 5 years ago

im also getting same issue with Version 4.5.1.

fabiano-bd commented 5 years ago

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 "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("", "");" to "postman.setEnvironmentVariable("", "md5value");". 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.

lucas937 commented 4 years ago

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

TaukSnarkyAgrud commented 4 years ago

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.

coditva commented 4 years ago

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
ziazon commented 4 years ago

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

Madhusudanpatidar commented 4 years ago

@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.....

Fez29 commented 4 years ago

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

bhargavi1716 commented 4 years ago

Do we have a solution on this yet?

bhargavi1716 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

Does this issue got resolved ?

Poizrak commented 4 years ago

@coditva Hello may I ask how's that implementation is going for this feature? It is really important for automated API testing...

PVoola commented 3 years ago

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

poponuts commented 3 years ago

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.

shraddhachheda-tkxs commented 2 years ago

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.

shraddhachheda-tkxs commented 2 years ago

Incase, any one is still wondering on this, using collection variables instead of environment variables works with Newman

ambatijsv commented 2 years ago

Any update on the above issue, is it fixed ..?

tiksang16 commented 1 year ago

Any update? Is there any way that could update the initial value?

Poizrak commented 1 year ago

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 ^^

zuluaica18 commented 1 year ago

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]

sshahrozahmadd69 commented 1 year ago

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; }`

blaubleu commented 8 months ago

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 :)