如何解决函数正在返回未定义的值
我在下面定义了一个函数。该函数将文件一个一个地下载并移动到名为templates的目录中。最后,它应该返回目录的长度。问题是,它返回 undefined
值。我相信我在搞砸一些事情。有人可以帮忙吗?
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() 定义为
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;
}
函数返回 undefined
值。
it('test if templates are downloadable',() => {
var templates = bt.no_of_templates();
expect(templates).toBe(6);
});
解决方法
因为您在每个中都使用了异步函数,所以它不会等待它们完成。
试试这个:
this.no_of_templates = async function () {
var num;
var holdon = this.download_files.map( async function (elem) {
wait.waitForElementVisibility(elem);
js.highlighterElement(elem);
elem.click();
return 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;
}
});
});
await Promise.all(holdon)
return num;
}
测试:
bt.no_of_templates().then(num => {
expect(num).toBe(6);
});
只需要更改以匹配您的框架,它似乎不是纯javascript。
我在代码中发现了一些问题。 为什么每次下载任务都要计算文件数?
,它抛出错误
(node:12451) UnhandledPromiseRejectionWarning: TypeError: [object Object] is not iterable
at Function.all (<anonymous>)
at BusinessTemplatesPage.no_of_templates (/home/vivek/simplifiedcredit-qa-automation/src/page-objects/business_templates.page.js:125:23)
at UserContext.it (/home/vivek/simplifiedcredit-qa-automation/src/specs/business_templates.spec.js:41:26)
at /home/vivek/simplifiedcredit-qa-automation/node_modules/jasminewd2/index.js:112:25
at new ManagedPromise (/home/vivek/simplifiedcredit-qa-automation/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1077:7)
at ControlFlow.promise (/home/vivek/simplifiedcredit-qa-automation/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2505:12)
at schedulerExecute (/home/vivek/simplifiedcredit-qa-automation/node_modules/jasminewd2/index.js:95:18)
at TaskQueue.execute_ (/home/vivek/simplifiedcredit-qa-automation/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:3084:14)
at TaskQueue.executeNext_ (/home/vivek/simplifiedcredit-qa-automation/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:3067:27)
at asyncRun (/home/vivek/simplifiedcredit-qa-automation/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2974:25)
(node:12451) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。这个错误要么是因为在没有 catch 块的情况下抛出了异步函数,要么是因为拒绝了一个没有用 .catch() 处理过的承诺。 (拒绝编号:2) (节点:12451)[DEP0018] 弃用警告:不推荐使用未处理的承诺拒绝。将来,未处理的承诺拒绝将以非零退出代码终止 Node.js 进程。
因为如果我不计算它会给出预期的结果
- Expected 1 to be 6.
- Expected 2 to be 6.
- Expected 3 to be 6.
- Expected 3 to be 6.
- Expected 5 to be 6.
而且我必须使用相同的功能来测试不同类别的模板。目前有4个类别,每个类别都有不同数量的模板。
,this.download_files
是一个 ElementArrayFinder。
this.download_files = element.all(by.xpath('//button//img[@src="url_here"]'));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。