Global function issue

Team

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.

  1. 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.
  2. In the test, I am calling the global function
  3. It is not evaluating the function correctly
  4. Can I define the function once and call it from each test in the collection (sort of folder level or collection level function) ?
  5. 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ā€

Hi there @rjayaram -

Looks like this question has been asked already (Global functions via Collection level folder? , Where is the best place to set global functions for tests?). Can you see if any of these threads answer your questions? Or weigh in on either thread?

Hi Joyce

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, Raje J

imagelink_check_status=eval(globals.linkcheck_Function)(imageurl);

Not sure youā€™re passing the parameter correctly. Does it work if you update the above line of code to:

let imagelink_check_status = eval(globals.linkcheck_Function)
imagelink_check_status(imageurl)

Hi Joyce

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
Pre-req script

postman.setGlobalVariable(ā€˜linkcheck_Functionā€™, (link)=> {

  • pm.sendRequest(link, function(err, resp) {*
  •   // pm.expect(err).to.not.be.ok;*
    
  • console.log("In the function status= "+resp.status);*
  • return resp.status;*
  • });*
    })

Test tab (updated per your suggestion)
let imagelink_check_status=eval(globals.linkcheck_Function)

  •        imagelink_check_status(imageurl);*
    
  •        console.log("image link and check status = "+imageurl + " " +imagelink_check_status);*
    
  •        pm.environment.set("url_reachable",false);*
    

At the end of the test, I have:
pm.environment.unset(ā€œurl_reachableā€);

@rjayaram pm.sendRequest is an asynchronous function, you canā€™t simply return a value from there.

Consider writing your test inside the pm.sendRequest function. Example:

pm.sendRequest('https://postman-echo.com/get', (error, response) => {
    if (error) { 
      console.log(error); 
    }

  pm.test('response should be okay to process', () => {
    pm.expect(error).to.equal(null);
    pm.expect(response).to.have.property('code', 200);
    pm.expect(response).to.have.property('status', 'OK');
  });
});

Many users get confused about scripts and eval. You can do this much cleaner by using a normal postman request where you pass the URL as a variable.

Hi Vdespa

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.