繁体   English   中英

使用 node 运行 newman (Postman) 集合时如何处理异步调用?

[英]How do I handle asynchronous calls when running newman (Postman) collections with node?

我可以用 bash 做到这一点,但我正在尝试学习 node 并希望从那里开始。 如何让 newman run 调用同步。 我不太了解 async/await 的使用(如果这是这里所需要的)。 我有以下脚本循环遍历一堆集合文件(每个文件包含多个请求)并调用 newman 在每个文件上运行:

// node imports
const fs = require('fs');
const newman = require('newman');

// test variables
const testFolder = './api-tests/';

// read all files in the test folder
fs.readdirSync(testFolder).forEach(file => {
    console.log('Running file: ' + file);

    // run newman using the file
    newman.run({
        collection: require(testFolder + file),
        delayRequest: 500,
        iterationData: [
            {
                'host': 'localhost',
                'port': '8080'
            }
        ],
        reporters: ['cli', 'html']
    }, (err, summary) => {
        if (err) {
            throw err;
        }
        console.log(file + ' run complete');
    });
});

Newman 立即执行每个文件,而不是等待循环返回到下一个文件。

谢谢。

你可以使用 deasync https://github.com/abbr/deasync

    var done = false;
    fs.readdirSync(testFolder).forEach(file => {
      newman.run({
      ...
      }).on('start', function (err, args) { // on start of run, log to console
            console.log('running a collection...');
      }).on('done', function (err, summary) {
            ...
            done = true;
      });
      require('deasync').loopWhile(function(){return !done;});
      done = false;
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM