javascript Postman 中的外部库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31157749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:22:30  来源:igfitidea点击:

External library in Postman

javascriptpostman

提问by UserControl

I want to use linq.jsfor my assertions. Is there a way to include an external library in Postman?

我想将linq.js用于我的断言。有没有办法在 Postman 中包含外部库?

回答by elssar

No, linq.js, or any library not available in the Postman Sandboxcannot be used in Postman (by default, there is a workaround).

不可以,linq.js或者任何在Postman Sandbox中不可用的库都不能在Postman中使用(默认情况下,有一个解决方法)。

EDIT

编辑

Actually, if you fetch a script in a request and evalit, you can use it in Postman. An example is given in this blog post - http://blog.getpostman.com/2015/09/29/writing-a-behaviour-driven-api-testing-environment-within-postman/

实际上,如果您在请求中获取脚本并获取eval它,则可以在 Postman 中使用它。此博客文章中给出了一个示例 - http://blog.getpostman.com/2015/09/29/writing-a-behaviour-driven-api-testing-environment-within-postman/

回答by Mario

I am doing pretty much the same than @grinderX19.

我所做的与@grinderX19 几乎相同。

I run this once to load my global variable:

我运行一次以加载我的全局变量:

postman.setGlobalVariable("myUtils", function myUtils() {
let utils = {};

utils.function1= function function1(Arg1, Arg2){
    <code>
};

utils.function2= function function2(Arg1, Arg2){
    <code>
};

return utils;
} + '; myUtils();'
);

Then, I am calling it like this in Postman requests:

然后,我在 Postman 请求中这样称呼它:

//import the global variable
let utils = eval(globals.myUtils);

//Call a function contained by this global variable
var var1 = utils.function1(arg1, arg2);

Hope this helps.

希望这可以帮助。

回答by grinderX19

There's an open feature for that in Postman's bugtracker since 2015: Loading External JS Files #1180but it doesn't seem they're actively working on it.

自 2015 年以来,Postman 的 bugtracker 中有一个开放的功能:加载外部 JS 文件 #1180,但他们似乎并没有积极地处理它。

Meanwhile I'm using a workaround mentioned in one of the commentsby putting a minimized custom JS in a global variable and loading it in the beginning of each script where I'm using this code:

同时,我正在使用其中一条评论中提到的解决方法,将最小化的自定义 JS 放入全局变量中,并将其加载到我使用此代码的每个脚本的开头:

eval(postman.getGlobalVariable("environment variable key"));