【问题标题】:Accessing environment variable in Newman script在 Newman 脚本中访问环境变量
【发布时间】:2021-12-02 13:14:20
【问题描述】:

我想在每次 Newman 发送请求时从我的环境中访问变量并将其保存到文件中。我知道从命令行运行时有--export-environment 选项,但我需要像邮递员一样通过pm.environment.get("name") 访问它,所以最终脚本看起来像这样

newman.run({
    collection: require('./collection.json'),
    reporters: 'cli',
    environment: require('./environment.json'),
    exportEnvironment: require('./environment.json'),
    globals: require('./globals.json'),
    exportGlobals: require('./globals.json')
})
.on('request', (error, data) => {
    if (error) {
        console.log(error);
        return;
    }

    var variable1 = 'first variable' 
    var variable2 = 'second variable'

    fs.appendFile("my_values.txt", variable1 + variable2, function (error) {
        if (error) { 
             console.error(error); 
        }
     });
});

【问题讨论】:

  • 只是出于好奇,这里的用例是什么?
  • @DannyDainton 将部分数据保存到文件并使用 bash 进行处理。我可以用我目前做的响应数据来做到这一点,但我需要从测试中调用另一个请求,它会覆盖原始响应

标签: javascript node.js postman newman


【解决方案1】:

我找到了答案,所以我会发布它,因为文档中没有关于此的任何内容,也无法在任何网站上找到答案。看起来变量可以在 test 这样的事件中访问

newman.run({
    collection: require('./collection.json'),
    reporters: 'cli',
    environment: require('./environment.json'),
    exportEnvironment: require('./environment.json'),
    globals: require('./globals.json'),
    exportGlobals: require('./globals.json')
}).on('test', (error, data) => {
    if (error) {
        console.log(error);
        return;
    }
    var variable1 = data.executions[0].result.environment.values.reference.myVariable1.value;
    var variable2 = data.executions[0].result.environment.values.reference.myVariable2.value + '\n';

    fs.appendFile("output.txt", variable1 + variable2, function (error) {
        if (error) { 
             console.error(error); 
        }
     });
});

【讨论】:

    猜你喜欢
    • 2019-12-24
    • 1970-01-01
    • 2016-03-22
    • 1970-01-01
    • 2010-11-07
    • 2015-03-11
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多