I am exploring global functions. A function that has to be called by many requests in the same collection as well by many requests in the same tests.
For example, I have 10 requests in the collection. Each request when executed, returns huge JSon response. I am parsing the response and taking only the image link from the response. I need to validate if the image link is working or not.
Appreciate your input on the following issue and looking for efficient way to handle this.
As I have to validate image link (to make sure the link is reachable or not) from all of the 10 requests, I thought of putting the link validation function in the pre-request script as a global function.
In the test, I am calling the global function
It is not evaluating the function correctly
Can I define the function once and call it from each test in the collection (sort of folder level or collection level function) ?
OR do I need to repeat the same global function in each request’s pre-req tab?
Pre-req script
postman.setGlobalVariable(‘linkcheck_Function’, (link)=> {
pm.sendRequest(link, function(err, resp) {
console.log("In the function status= "+resp.code);
//This is printing the status code correctly inside the function
return resp.code;
});
});
In the test tab of the same request
var imagelink_check_status="";
//—code block here to process and parse the response and saving the link in the imageurl------
console.log("image url = "+imageurl);
imagelink_check_status=eval(globals.linkcheck_Function)(imageurl);
console.log("image link and check status = "+imageurl + " " +imagelink_check_status);
//imageurl is printed correctly on the console but the imagelink_check_status is undefined
Results
image url is printed correctly but the status is “undefined”
Thanks for looking into this issue. I have looked into the thread that you referenced. As suggested in the reply I was trying to define a global function and evaluating the value of the function in the test. The only difference is, I need to call this function from my test many times. As stated in my problem earlier, the value of the function is not evaluated correctly. Pl see my code snippet from test tab and pre-req script.
Thanks for your suggestion. I have updated as below. Looks like that function is now getting executed. I see In the function status= OK in the console. However the env variable “url_reachable” is always set to false. Looks like it did not understand unset command at the end.
let me know if I am missing something.
Thanks for looking into this. I knew it is a asynchronous function. The reason why I prefer a global function is, I have say 10 requests in the collection. Each request has many tests. Each response gives “N” number of urls. Hence instead of repeating pm.sendRequest N times, I would like to declare that as a function and call it in the loop when iterating over the response array. Let me know efficient and simple solution to achieve this. All I need to do is validate the dynamic urls received from each request. If the url is not valid, then I need to write it in the console log and update the env variable /flag.