【问题标题】:postman: changing pre-request script dyncamically during a collection run邮递员:在收集运行期间动态更改预请求脚本
【发布时间】:2021-03-17 20:32:54
【问题描述】:
对于某些请求,我想将某些函数作为预请求脚本运行,而不是其他请求。我想这样做,而不必为使用 UI 的所有请求重复代码,但在一个地方 - 也许是集合的预请求脚本。沙箱是否允许访问运行时,以便我可以将其定义为第一个请求的预请求脚本。
类似currentRun.collection['someRequest'].pre = () => {console.log("hello world!")}
【问题讨论】:
标签:
scripting
postman
postman-collection-runner
【解决方案1】:
单击集合并在其中添加预请求脚本。
在collection pre-request里面,可以加if条件
if (pm.info.requestName==="request1"){
do this
} else if (pm.info.requestName==="request2"){
do this
}
另一种选择是将函数保存为环境变量并在其上调用 eval:
eval(pm.environment.get("variablename"))