Test data from external sources (e.g CSV, DB etc.) can be passed to one or multiple tests with CLI using the config file.

  1. Add a JavaScript function that loads data from an external source into Json objects. Add this function in the beginning of the config file.
  2. Use overrideTestData in the beforeSuite to pass test names and their data sets.
  3. Install the npm package csvtojson in order to use this functionality. See more details here: https://www.npmjs.com/package/csvtojson

Here is an example :

// JS function that loads data from CSV into Json objects

const csvtojson = require("csvtojson");
function loadCsvFile(path) {
    return new Promise((resolve) => {
        return csvtojson()
            .fromFile(path)
            .then(resolve, err => {
                console.error("failed to read csv file", err.message);
                resolve([]);
            });
    });
}
beforeSuite: function () {
    return Promise.all([loadCsvFile('./data.csv'),
loadCsvFile('./data2.csv'), loadCsvFile('./data3.csv')])
        .then(([jsonObj, jsonObj2, jsonObj3]) => {
            return {
                BEFORE_SUITE: "BEFORE_SUITE",
                overrideTestData: {
                "Test 3": jsonObj,
                "Test 4": jsonObj2,                        
                "Test 5": jsonObj3,                        
                // You can also pass static datasets to tests
                "Test 6": {name: "ryan"}                    
            }
        }
    });
} //add comma here if there are more functions after beforeSuite

Notes: 

  • Test data provided in the beforeSuite() hook in the config file overrides test data provided in the UI.
  • In the csv file the first row (header row) will contain the data keys and will be used as parameter names, and each row afterwards will contain a dataset of values.
Did this answer your question?