简体   繁体   English

Function 正在返回未定义的值

[英]Function is returning the undefined value

I have a function defined below.我有一个 function 定义如下。 This function downloads files one by one and moves it to the directory named templates.这个function一个一个下载文件,移动到templates目录下。 In the end, it should return the length of the directory.最后,它应该返回目录的长度。 The issue is, it returns the undefined value.问题是,它返回undefined的值。 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 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;
}

Function returns undefined value. Function 返回undefined的值。

it('test if templates are downloadable', () => {
var templates = bt.no_of_templates();
expect(templates).toBe(6);

}); });

Because you are using async function in each, it won't wait for they finished.因为您在每个中都使用异步 function,所以它不会等待它们完成。

try this:尝试这个:


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;
}

test:测试:

bt.no_of_templates().then(num => {
  expect(num).toBe(6);
});

just need to change to match your framework, it seems not pure javascript.只需要更改以匹配您的框架,似乎不是纯 javascript。


And i found some problem in code.我在代码中发现了一些问题。 why are you count files in every download job?为什么要在每个下载作业中计算文件?

It throws the error它抛出错误

(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: Unhandled promise rejection. (节点:12451) UnhandledPromiseRejectionWarning:未处理的 promise 拒绝。 This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with.catch().此错误源于在没有 catch 块的情况下抛出异步 function 内部,或拒绝未使用.catch() 处理的 promise。 (rejection id: 2) (node:12451) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. (拒绝 id:2)(节点:12451)[DEP0018] DeprecationWarning:不推荐使用未处理的 promise 拒绝。 In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.将来,未处理的 promise 拒绝将使用非零退出代码终止 Node.js 进程。

because if I don't count it gives the expected result as因为如果我不计算它会给出预期的结果

- 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.

And I have to use the same function to test templates of different categories.而且我必须使用相同的function来测试不同类别的模板。 Currently there are 4 categories and each one has different number of templates.目前有 4 个类别,每个类别都有不同数量的模板。

this.download_files is an ElementArrayFinder. this.download_files是一个 ElementArrayFinder。

this.download_files = element.all(by.xpath('//button//img[@src="url_here"]'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM