[英]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) : 我的函数实现就像(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);
问题在于函数正在返回“未定义”值,并且打印语句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
: 您需要解决承诺并在其中记录结果, 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() {
...
});
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.