👉 Want to learn more about Postman? Check my Postman online course. Get it at a special price and help support this RUclips channel: vdespa.com/courses/?q=RUclips
You can also declare functions inside an object on the Pre-request script of the Collection header, and all requests under that collection can use those functions without eval. The only caveat is you cannot use pm inside the function body, but you can pass pm as function parameter.
Awesome. I was going a step further to have all common functionality in a separate collection and then call the common() method using eval from another collection within the same environment. Trying this, I'm getting "eval not a function" error. I tried with both environment and globals and not having any luck. Is this not possible? Can you please walk us thru on doing this way?.
Valentin, I am a beginner so don't be hard on my question please) Why did you place the tests to the pre-request script? I thought tests should always be executed after the request.
The tests are in the pre-request script but are not executed there. Maybe this concept is a bit harder to grasp in the beginning. If you are just getting started with Postman, consider taking a course.
I tried this on a soap request, after adding the get statement in test tab of another request i get the message "There was an error in evaluating the test script: TypeError: Cannot read property 'MC' of undefined". Same thing works absolutely fine when the get statement is in the same request api in test tab as that of the var created and set statement.
@@vdespa I have gone through your course on Udemy, but no where have I seen anything about soap calls. Do you want to say that the above approach is not suitable for soap requests?
Doesn't seem to work with Postman 7.0.6 ;commonTests(); throws error as: There was an error in evaluating the test script: ReferenceError: commonTests is not defined
can I use this for pre-request, reuse a pre-request in multiples request? if I have , pre-request and test, but I only want to reuse the pre-request , how can i do it?
This trick is not satisfactory for me. First of all, the definition of function taking parameters does not seem feasible in a reasonable way. Second, the definition of the "shared" tests is strongly tied to the call in which one defines them. This means that I need to run this call first every time. The video is from 2017, I frankly find it hard to believe that nobody needed to add own JS code to their projects. Having to use this hack in 2020 reduces, in my opinion, the usefulness of Postman by a significant amount.
I am not a big fan of this either, Giuseppe. But I don't know a better way to do it. I agree, this is 2020 and the app should allow more. Feel free to submit an issue with Postman github.com/postmanlabs/postman-app-support/issues/
👉 Want to learn more about Postman? Check my Postman online course. Get it at a special price and help support this RUclips channel:
vdespa.com/courses/?q=RUclips
So much better than beating the bush around StackOverflow!
Thanks a lot Valentin!!!!
You can also declare functions inside an object on the Pre-request script of the Collection header, and all requests under that collection can use those functions without eval. The only caveat is you cannot use pm inside the function body, but you can pass pm as function parameter.
Not sure what you mean. Can you share an example or more details?
Hi Velentin
Can you make video on how to re-use functions in javascript that take in arguments as well? I'm having trouble setting that up
Awesome. I was going a step further to have all common functionality in a separate collection and then call the common() method using eval from another collection within the same environment. Trying this, I'm getting "eval not a function" error. I tried with both environment and globals and not having any luck. Is this not possible? Can you please walk us thru on doing this way?.
hi, may i know what is the purpose for the extra bracket behind the eval()?
eval(pm.environment.get(""))();
Sure. That is a function call.
I tried it . Feel good to optimize the code
Great to hear!
How can refer an external file in POSTman script.. Eval is evil may not be allowed by sensitive tools?
You can't read from the filesystem from Postman scripts.
Valentin, I am a beginner so don't be hard on my question please) Why did you place the tests to the pre-request script? I thought tests should always be executed after the request.
The tests are in the pre-request script but are not executed there. Maybe this concept is a bit harder to grasp in the beginning.
If you are just getting started with Postman, consider taking a course.
@@vdespa thank you, I am already considering this. But could you please answer, why would you not leave the tests in the Tests tab?
@@freshrace1 The tests are stored in an environment variable, so it does not matter where they are in initially.
@@freshrace1 Write me a message on LinkedIn. I can help you.
why environment variable, why not use global/collection etc? Is there a rule or convention?
Use whatever makes more sense for your use-case. I was just demonstrating the principle.
I tried this on a soap request, after adding the get statement in test tab of another request i get the message "There was an error in evaluating the test script: TypeError: Cannot read property 'MC' of undefined". Same thing works absolutely fine when the get statement is in the same request api in test tab as that of the var created and set statement.
You need to understand some basics around JavaScript. Check this RUclips channel for a full course.
@@vdespa I have gone through your course on Udemy, but no where have I seen anything about soap calls. Do you want to say that the above approach is not suitable for soap requests?
@@SandyShiralkar Please send me a private message with what you have tried and where you got stuck. I need a bit more context.
Gracias, me ahorra mucho código. Thanks, this will reduce my code.
Doesn't seem to work with Postman 7.0.6 ;commonTests(); throws error as:
There was an error in evaluating the test script: ReferenceError: commonTests is not defined
@@vdespa any update on how to make it work?
can I use this for pre-request, reuse a pre-request in multiples request?
if I have , pre-request and test, but I only want to reuse the pre-request , how can i do it?
@@vdespa thank you
This trick is not satisfactory for me. First of all, the definition of function taking parameters does not seem feasible in a reasonable way. Second, the definition of the "shared" tests is strongly tied to the call in which one defines them. This means that I need to run this call first every time. The video is from 2017, I frankly find it hard to believe that nobody needed to add own JS code to their projects. Having to use this hack in 2020 reduces, in my opinion, the usefulness of Postman by a significant amount.
I am not a big fan of this either, Giuseppe. But I don't know a better way to do it. I agree, this is 2020 and the app should allow more. Feel free to submit an issue with Postman github.com/postmanlabs/postman-app-support/issues/
Did you find a solution to this?
David Quick nope. This limits the extent to which I use Postman.
Hacky, Postman is disappointing. These workarounds for re-using requests and functions and test scripts should be built in app functionality.
I agree, Jeremy. Unfortunately Postman has not done much since this video was posted to address this problem.