简体   繁体   中英

asynchronous execution in protractor end to end tests

I have written a function , which is returning a value. In my main i am calling the function like this:

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

And my function implementation is like(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();
                }
            });
        });
    }

};

The problem is function is returning "undefined" value, and the print statement console.log("returned value is : " + fn_return_val); is executing before the function implementation

Can anyone help me on resolving this?

This is all about promises and protractor's Control Flow .

You need to resolve the promise and log the results inside then :

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

And you also need to return from a function:

function select_lesson(lesson_name) {

    ...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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