Reputation: 1812
I am trying to replace a body with a pre-request script in Postman/newman. For some reason, it works in Postman but not in newman.
So I have this pre-request script:
// pm.environment.set("NEW_CONF", "FOO"); // testing
eval(pm.environment.get("getConfigFunc"));
getConfig().then((conf) => {
pm.environment.set("CONF_BACKUP", JSON.stringify(conf));
conf.General.Port = conf.General.Port + 1;
// pm.environment.set("NEW_CONF", "BAR"); // testing
pm.environment.set("NEW_CONF", JSON.stringify(conf));
});
In the body there is just {{NEW_CONF}}
. Postman replaces it, but in newman it does not work.
As you can see I added settings the variable NEW_CONF to FOO and later to BAR. FOO gets set and replaces the body, BAR is never set.
getConfig runs a pm.sendRequest which returns a promise with the resp.json() as return.
What am I doing wrong? Is newman not waiting for the promise? If so, is there a way to tell newman when to run the request after the promise returns?
Upvotes: 1
Views: 239
Reputation: 23
I had a similar problem and I found a solution on StackOverflow: the reason is that Postman does not fully support async/await/Promises, but fully supports setTimeout, so if you set setTimeout to 1000 (ms) or 10000 (ms) (which should be enough), Postman will wait and your promises will resolve. Then you need to clear the timeout.
Here is the solution: How to use Promises in Postman tests?
And here a general thread about it: https://github.com/postmanlabs/postman-app-support/issues/4131
Hope it helps.
Upvotes: 0