I am trying to run data-driven testing using csv file in protractor:
I have created a read csv promise module:
let readSync = async function (path1) {
console.log(path1)
const csv = require('csv-parser')
const fs = require('fs')
const results = [];
function getIdRequest(path2) {
console.log(path2)
return new Promise((resolve, reject) => {
fs.createReadStream(path2)
.pipe(csv(path2))
.on('data', (data) => results.push(data))
.on('end', () => {
resolve(results);
})
});
}
console.log(path1)
return getIdRequest(path1);
}
// to get the request body
module.exports = readSync;
Now i am calling the same from my test file:
let readCSV = require('../commons/readSync.js');
describe('Validate stage 1 behaviour', function () {
let testdata=await readCSV(browser.params.testFile);
for (let a of testdata) {
it('test {Regression} {Sanity} {Sanity}', async function () {
//check title
console.log("inside")
await mainPage.goto();
await mainPage.clickElement('inputField')
But i cannot use await inside describe, so I tried using :
let readCSV = require('../commons/readSync.js');
describe('Validate stage 1 behaviour', function () {
readCSV(browser.params.testFile).then(function(testdata){
for (let a of testdata) {
console.log("outside")
it('test {Regression} {Sanity} {Sanity}', async function () {
//check title
console.log("inside")
});
}
but in this, 'it' function is not getting executed, its printing only 'outside'
Challenges
Cannot use await inside describe as it accepts only synchronous function
Cannot use
promise.then(describe(function(){}
because 'IT' and 'Describe' is not recognized when called inside the callback. I get 0 spec executed message.Tried calling the testdata in before hooks but this also fails as the for loop is outside the It block . So, the compiler tries to run for loop even before 'before hook' is executed .
I tried using nested describe and calling await testdata in before each and before all . But even that fails with the same reason , refer: https://stackoverflow.com/q/48344674/6793637
Questions:
So is there a better way to run data driven testing using csv in protractor ?
Is there a way to synchronously make the suggest solution work?
Why is describe or IT block doesn't work inside a callback ? Eg Promise.then(describe()) won't execute describe