[使用nodejs从newman加载csv

问题描述 投票:-1回答:1

我有下面的代码正在运行。

我有一个使用javascript的脚本,该脚本使用nodejs并读取一个csv文件,但我不知道如何在newman.run中使用csv中的数据

var fs = require('fs');
var people = [];
var fileContents = fs.readFileSync('users.csv');
var lines = fileContents.toString().split('\n');
const newman = require('newman'); // require newman in your project
for (var i = 0; i < lines.length; i++) {
    people.push(lines[i].toString().split(','));
}
for (var i = 0; i < lines.length; i++) {

    console.log(people[i][0]);

    newman.run({
        collection: require('collection.json'),
        environment: require('environment.json'),   
        reporters: 'htmlextra',     
    }, function (err) {
        if (err) { throw err; }
        console.log("i"+'collection run complete!');
    });

}

我试图解释发生了什么。

我正确地遍历了csv文件,但是我不知道如何在有关post的正文中传递csv的值。

海葵能帮助我吗?

node.js postman newman
1个回答
0
投票

要从脚本中将datafile与Newman一起使用-您只需将iterationData键添加到newman.run对象,然后引用文件名作为值。

const newman = require('newman');

newman.run({
    collection: require('collection.json'),
    environment: require('environment.json'),
    iterationData: '<path to file>/users.csv',   
    reporters: 'htmlextra',     
}, function (err) {
    if (err) { throw err; }
    console.log('collection run complete!');
});

要使用请求中的值,您需要使用引用CSV列标题的{{var_name}}语法添加此变量名。

可以在这里找到有关使用数据文件的更多信息:

https://learning.getpostman.com/docs/postman/collection-runs/working-with-data-files/

© www.soinside.com 2019 - 2024. All rights reserved.