I need some assistance with a function I have below. This function is meant to download files one by one and move them to a directory called templates. At the end, it should return the length of the directory. However, it seems to be returning an undefined value. I suspect that I am missing something here. Can anyone provide some guidance?
this.countTemplates = 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;
}
The method getAllDirFiles() is defined as follows:
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;
}
The function currently returns an undefined value.
it('testing if templates are downloadable', () => {
var templates = bt.countTemplates();
expect(templates).toBe(6);
});