繁体   English   中英

量角器端到端测试中的异步执行

[英]asynchronous execution in protractor end to end tests

我已经编写了一个函数,该函数返回一个值。 在我的主要我叫这样的功能:

var fn_return_val = lesson.select_lesson(lesson1_text);  
console.log("returned value is : " + fn_return_val);

我的函数实现就像(other file.js)一样:

module.exports = {
 select_lesson:
    function select_lesson(lesson_name) {

        console.log('\n ************************* Lessson name: ' + lesson_name);
        var desiredOption, status;
        var repeter = element.all(by.repeater('item in items'));

        repeter.then(function (items) {
            items.forEach(function (icon) {
                console.log('\n ************************* item');
                icon.getText().then(function (txt) {
                    if (txt == lesson_name) {
                        desiredOption = icon;
                    }
                })
            }).then(function clickOption() {
                if (desiredOption) {
                    var el = desiredOption.all(by.css('[ng-click="launchActivity()"]'));
                    var el_progress = desiredOption.all(by.css('.pna-progress'));
                    var abc = el.getAttribute('value').then(function (txt) {
                        status = txt;
                        return status
                    });
                    el_progress.getAttribute('style').then(function (progress) {
                        console.log('\n ************************* Lessson progress : ' + progress);
                    });
                    el.click();
                }
            });
        });
    }

};

问题在于函数正在返回“未定义”值,并且打印语句console.log("returned value is : " + fn_return_val); 在函数实现之前执行

谁能帮助我解决这个问题?

这全部与承诺和量角器的控制流程有关

您需要解决承诺并在其中记录结果, then

lesson.select_lesson(lesson1_text).then(function(fn_return_val) {
    console.log("returned value is : " + fn_return_val);
});

而且您还需要从函数返回:

function select_lesson(lesson_name) {

    ...

    // return here
    return repeter.then(function (items) {
        ...
        }).then(function clickOption() {
            ...
        });
    });
}

暂无
暂无

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

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