问题描述
我需要帮助编写一个通用函数以在一组请求中使用,这将有助于构建框架.
I need help writing a common function to use across a collection of requests which will help with building a framework.
我尝试过使用以下格式
以下函数在第一个函数的Test选项卡中声明
The following function is declared in the Test tab in the first function
postman.setGlobalVariable("function", function function1(parameters)
{
//sample code
});
我在预请求中使用了以下内容
I used the following in the pre-request
var delay = eval(globals.function);
delay.function1(value1);
我收到以下错误
评估预请求脚本时出错:无法读取未定义的属性function1".
谁能帮我定义全局/通用函数并在请求中使用它们?
Can anyone help me with how to define Global/common functions and use them across the requests?
提前致谢
推荐答案
没有eval:
在集合的预请求脚本中定义一个包含您的函数的对象,而不使用 let、var 等.这会将其附加到 Postman 的全局沙箱对象.
Define an object containing your function(s) in the collection's pre-request scripts without using let, var, etc. This attaches it to Postman's global sandbox object.
utils = {
myFunc: function() {
return 'hello';
}
};
然后在您请求的预请求或测试脚本部分中调用该函数:
Then within your request's pre-request or test script section just call the function:
console.log(utils.myFunc());
这篇关于如何在 Postman 中编写全局函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)