I am using a global env file to have some functions in.
To call those functions in my test I use
const globalFunction = eval(globals.globalFunction)();
globalFunction.execute.GetAccountNumber();
Now, every request has to have the
const globalFunction = eval(globals.globalFunction)();
so I can use my function. Rather than having to add this in each request. I am trying to call it in the collections prereq script. So called before each test.
However, it doesn’t seem to work.
I get the error
There was an error in evaluating the Pre-request Script: ReferenceError: globalFunction is not defined
What is my problem??
Hey @jasoncaunt
Welcome to the Postman community!
Would you able able to share more about the function itself and maybe a screenshot of the different areas that you have that in the app?
Just trying to visualize what you’re doing
Also, would getting the function this way help?
eval(pm.globals.get("globalFunction"))
This thread might add some extra information into getting something working:
I am very sorry if this has already been covered. I have found conflicting information online and just want to know if this is an actual available feature or not.
I want to know if it’s possible to have global functions at the Collection level folder (either in the pre-request or tests section) where I can store any functions I use for nearly every test.
For example say for every request I do the following:
console.log(‘hello!’);
could I instead create a function in say the pre-request secti…
My function looks like
var CheckResponseCount = (retryDelay, retryLimit, expectectedCount, retryCount = 1) => {
var bearer_token = pm.environment.get("bearer_token");
// Construct our request URL from environment variables
var baseurl = request["url"].replace("{{BaseURL}}", pm.environment.get("BaseURL"));
baseurl = baseurl.replace("{{AccountId}}", pm.environment.get("AccountId"));
pm.sendRequest({
url: baseurl,
method: 'GET',
header: {
'content-type': 'application/json',
'authorization': 'Bearer ' + bearer_token
}
}, function (err, response) {
if (err) {
console.log("ERROR:" + err);
} else {
if (response.json().length !== expectectedCount) {
if (retryCount < retryLimit) {
console.log('Job is still PENDING. Retrying in ' + retryDelay + 'ms');
setTimeout(function () {
CheckResponseCount(retryDelay, retryLimit, expectectedCount, ++retryCount);
}, retryDelay);
} else {
console.log('Retry limit reached, giving up.');
pm.test(pm.info.requestName + " - FAILED TO GET CORRECT RESPONSE COUNT", function () {
pm.expect(response.json().length).to.eql(!expectectedCount)
});
postman.setNextRequest(null);
}
}
if (response.json().length === expectectedCount) {
pm.test(pm.info.requestName + " - Correct Response Count", function () {
pm.expect(response.json().length).to.eql(expectectedCount)
});
}
}
});
}
I currently call this function in my pre-req of my request
const globalFunction = eval(globals.globalFunction)();
globalFunction.execute.CheckResponseCount(5000,15,1);
But instead of having the
const globalFunction = eval(globals.globalFunction)();
in every request in the collection. I want it just once in the Collections Pre-Req section