【发布时间】:2015-09-18 09:35:28
【问题描述】:
我想将linq.js 用于我的断言。有没有办法在 Postman 中包含一个外部库?
【问题讨论】:
-
你最好直接问他们——他们很擅长回答问题...getpostman.com/support
标签: javascript postman
我想将linq.js 用于我的断言。有没有办法在 Postman 中包含一个外部库?
【问题讨论】:
标签: javascript postman
不,linq.js 或 Postman Sandbox 中不可用的任何库都不能在 Postman 中使用(默认情况下,有一种解决方法)。
实际上,如果您在请求中获取脚本并eval,则可以在 Postman 中使用它。此博客文章中给出了一个示例 - http://blog.getpostman.com/2015/09/29/writing-a-behaviour-driven-api-testing-environment-within-postman/
【讨论】:
我做的和@grinderX19 差不多。
我运行一次来加载我的全局变量:
postman.setGlobalVariable("myUtils", function myUtils() {
let utils = {};
utils.function1= function function1(Arg1, Arg2){
<code>
};
utils.function2= function function2(Arg1, Arg2){
<code>
};
return utils;
} + '; myUtils();'
);
然后,我在 Postman 请求中这样称呼它:
//import the global variable
let utils = eval(globals.myUtils);
//Call a function contained by this global variable
var var1 = utils.function1(arg1, arg2);
希望这会有所帮助。
【讨论】:
自 2015 年以来,Postman 的 bugtracker 中有一个开放功能:Loading External JS Files #1180,但他们似乎并没有在积极开发它。
同时,我使用one of the comments 中提到的解决方法,将最小化的自定义 JS 放入全局变量中,并将其加载到我使用此代码的每个脚本的开头:
eval(postman.getGlobalVariable("environment variable key"));
【讨论】:
我在https://postman-quick-reference-guide.readthedocs.io/en/latest/libraries.html#custom-libraries找到了解决方案
pm.sendRequest("https://example.com/your-script.js", (error, response) => {
if (error || response.code !== 200) {
pm.expect.fail('Could not load external library');
}
eval(response.text());
// YOUR CODE HERE
});
使用jalali-moment的示例:
pm.sendRequest("https://unpkg.com/jalali-moment/dist/jalali-moment.browser.js", (error, response) => {
if (error || response.code !== 200) {
pm.expect.fail('Could not load external library');
eval(response.text());
var currentTime=moment().locale('fa').format('YYYYMMDDHHmmss');
console.log(currentTime);
});
【讨论】: