如何在邮递员中编写全局函数 [英] How to Write Global Functions in Postman
问题描述
我需要帮助编写一个通用函数以在一系列请求中使用,这将有助于构建框架。
I need help writing a common function to use across a collection of requests which will help with building a framework.
我尝试使用以下格式
在第一个函数的测试选项卡中声明以下函数
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?
预先感谢
推荐答案
我使用这个小技巧:
pm.globals.set('loadUtils', function loadUtils() {
let utils = {};
utils.reuseableFunction = function reuseableFunction() {
let jsonData = JSON.parse(responseBody);
}
return utils;
} + '; loadUtils();');
tests['Utils initialized'] = true;
在另一个请求中,我可以重用全局变量 loadUtils
:
In another request I can reuse the global variable loadUtils
:
const utils = eval(globals.loadUtils);
utils.reuseableFunction();
您还可以检查邮差团队的开发人员路线图。集合级脚本已列入近期议事日程,应尽快使用,直到您可以使用显示的方法。
You can also check the developer roadmap of the Postman team. Collection-level scripts are on the near-term agenda and should be available soon until then you can use the shown method.
这篇关于如何在邮递员中编写全局函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!