Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The environment variable set in sendRequest (prerequest script) during Newman execution not accessible in testscript. Works in Collection Runner and Postman. #1825

Closed
slavenantic opened this issue Dec 17, 2018 · 17 comments
Assignees
Labels

Comments

@slavenantic
Copy link

  1. Newman Version (can be found via newman -v): 4.2.2
  2. OS details (type, version, and architecture): Windows 10 Pro, 64
  3. Are you using Newman as a library, or via the CLI? CLI
  4. Did you encounter this recently, or has this bug always been there: Recently
  5. Expected behaviour: To update the environment variable inside the sendRequest in the same manner as from Collection Runner and Postman
  6. Command / script used to run Newman: (pre-request script)
    `//
    let id1 = 19;
    let isDataDriven = pm.iterationData.get("Input");
    let id2 = 0;

if (isDataDriven) {
id1 = pm.iterationData.get("Input").id1;
id2 = pm.iterationData.get("Input").id2;
pm.environment.set('id2', id2);
}

var requestUrl = pm.environment.get("environment") + '/resource/' + id1;
var options = { method: 'GET', url: requestUrl };

pm.sendRequest(options, function (error, response) {
if (error) throw new Error(error);
var jsonData = response.json();
pm.environment.set('responseTime', response.responseTime);
if (id2 !== 0) {
id2 = jsonData.id2;
pm.environment.set('id2', id2);
}
});`
6. Sample collection, and auxiliary files (minus the sensitive details):
7. Screenshots (if applicable):

@codenirvana codenirvana self-assigned this Dec 17, 2018
@codenirvana
Copy link
Member

@slavenantic Can you share a sample collection and data file used to reproduce this issue?

@slavenantic
Copy link
Author

Is there any chance to send these things to you via email?

@codenirvana
Copy link
Member

@slavenantic You can attach the collection JSON file(minus the sensitive details) in GitHub comment itself.
For that collection, don't use your private endpoint, you can use any sample or echo domain.

@slavenantic
Copy link
Author

slavenantic commented Dec 19, 2018

Here they go.

sample.postman_collection.zip
The data file is zipped too.

@codenirvana
Copy link
Member

codenirvana commented Jan 3, 2019

@slavenantic Thanks for sharing the collection but Its hard to reproduce the issue using this collection. Since it has more than 50 requests as well as hitting a private endpoint.

Can you share the command used to run Newman?

@slavenantic
Copy link
Author

slavenantic commented Jan 3, 2019

There you go:
newman run "C:\PathToCollection\laplace.postman_collection.json" --folder "3_4_1_CheckRecipesWithImagePerformance" -e "C:\PathToEnvironment\qa.postman_environment.json" -d "C:\PathToFile\allImageIds_nl.json" -n 100 --reporters cli --disable-unicode > "C:\PathToReports\3_Images\3_4_PerformanceTests\3_4_1_CheckRecipesWithImagePerformance\cli\20190103135332\allImageIds_nl.txt"

@codenirvana
Copy link
Member

codenirvana commented Jan 3, 2019

@slavenantic Can you try executing the attached collection using:

$ newman run NEWMAN-1825.postman_collection.json -d allImageIds_nl.json

Modified your scripts to reproduce the issue locally. The environment variable recipeResponseTime is set in sendRequest correctly.

NEWMAN-1825.zip

@slavenantic
Copy link
Author

I can still reproduce the same issue with your modifications (I don't see any relevant changes in your collection). The variable is still set at the same place in pre-request script. What changes did you make actually?

@codenirvana
Copy link
Member

@slavenantic I just changed the request endpoint to an echo server to execute the collection.

Are tests failing for you? Can you try updating Newman to the latest version and check if the issue still persists?

@slavenantic
Copy link
Author

I am still having the same issue, even after newman update to 4.3.1.

@codenirvana
Copy link
Member

@slavenantic As discussed above, I am not able to reproduce this issue.
Check the attached collection in which a variable set in sendRequest(prerequest) is accessible in the test script.

NEWMAN-1825.json.zip

@praveendvd
Copy link

praveendvd commented Mar 7, 2020

@codenirvana I am facing the same issue,

I have uploaded the collection file, it has two requests one that works, one that doesn't.

I want to update the collection variable (or any variable) according to a few criteria and the status of the request ,

collection.zip

Please see the request description to know which works and which doesn't

@csk1827
Copy link

csk1827 commented Mar 7, 2020

@praveendvd you are updating the "projectid" variable in a promise in the test script of the second request, which is taking some time to execute. As promises are executed asynchronously, the getter of the "projectid" variable in (the last line of the test script) is getting the old value of the variable, before the execution of the promise. That's the reason why you are getting the old value for the "projectid" variable.

@csk1827
Copy link

csk1827 commented Mar 7, 2020

@codenirvana It might be the same case with @slavenantic too.

I have a question for you. Are the Postman sandboxes strictly sequential or they can run in parallel also? If they run in parallel, then some of the variables may not get assigned/updated in time(before the next sandbox tries to access that variable) if a request sent by sendRequest() in the first sandbox takes a long time to execute.

@praveendvd
Copy link

praveendvd commented Mar 7, 2020

@csk1827 yes that's the issue I was able to fix it


setTimeout(function() {

       pm.test("Status code is 200", function () {
              pm.response.to.have.status(200);
        });

       pm.test("Status code is 200", function () {
          pm.response.to.have.status(200);
          console.log("The collection varaible is")
         console.log(pm.collectionVariables.get('projectid'))
       });



}, 5000);

@shamasis
Copy link
Member

shamasis commented Mar 7, 2020

Sandboxes are supposed to be executed serially. The way sandbox waits for script execution completion is to look for timers to end. Since promises are not timers, they aren’t waited for. To wait for it, we need to deprecate till node v6 (and maybe 8) and use async_hooks to wait for timers. cc @codenirvana @coditva

@codenirvana
Copy link
Member

Closing as duplicate of postmanlabs/postman-app-support#5606

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants