I have a function defined below. This function downloads files one by one and moves it to the directory named templates. In the end, it should return the length of the directory. The issue is, it returns the undefined
value. I believe that I am messing up something. Can someone please help?
this.no_of_templates = function () {
var num;
this.download_files.each( async function (elem) {
wait.waitForElementVisibility(elem);
js.highlighterElement(elem);
elem.click();
browser.driver.wait(function(){
var filesArray = glob.sync(dirPath + '**/*.+(xlsx|docx|pptx)');
if(typeof filesArray !== 'undefined' && filesArray.length > 0){
return filesArray;
}
}, 60000).then(function(filesArray){
var filename = filesArray[0];
fileSystem.moveFile(filename, process.cwd()+'/templates/');
if(fileSystem.getAllDirFiles(process.cwd()+'/templates/').length >= 6){
num = fileSystem.getAllDirFiles(process.cwd()+'/templates/').length;
return false;
}
});
});
return num;
}
getAllDirFiles() is defined as
this.getAllDirFiles = function (dirPath, arrayOfFiles) {
var files = fs.readdirSync(dirPath);
arrayOfFiles = arrayOfFiles || [];
files.forEach(function (file) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = getAllDirFiles(dirPath + "/" + file, arrayOfFiles);
} else {
arrayOfFiles.push(file);
}
})
return arrayOfFiles;
}
Function returns undefined
value.
it('test if templates are downloadable', () => {
var templates = bt.no_of_templates();
expect(templates).toBe(6);
});