在循环 .each() 中使用 .then() 时,函数总是 return false
Function always return false when using .then() inside loop .each()
当从 Spec 调用函数时,代码是 运行 很好 'aria-label' 是 verified/checked 但 true/false 没有返回到 spec 来检查 .toBe(true )
页面对象:函数
checkOptionsEnabled: function(){
var status = false;
element.all(by.repeater('item in items')).then(function(options) {
options.forEach(function(option) {
option.getAttribute("aria-label").then(function(ariaLabel) {
expect(ariaLabel).toContain('enabled');
});
});
});
return status;
},
规范:调用函数
page.journeyModes.checkAll.click();
expect(page.journeyModes.checkOptionsEnabled()).toBe(true);
page.journeyModes.uncheckAll.click();
expect(page.journeyModes.checkOptionsDisabled()).toBe(true);
错误
1) TravelInfo As a user I can Select Journey Modes
Message:
Expected false to be true.
Stacktrace:
Error: Expected false to be true.
at new jasmine.ExpectationResult
C:\Users\sunil.sharma\Documents\PortalAutomatedTests\node_modules\protractor\n
de_modules\minijasminenode\lib\jasmine-1.3.1.js:137:32)
在 [对象对象].
你需要的是reduce()
:
checkOptionsEnabled: function() {
return element.all(by.repeater('item in items')).reduce(function(acc, elem) {
return elem.getAttribute("aria-label").then(function (attr) {
return acc && (attr.indexOf("enabled") >= 0);
});
}, true);
},
这基本上是检查 aria-label
属性中是否存在 enabled
并累积结果布尔值。
当从 Spec 调用函数时,代码是 运行 很好 'aria-label' 是 verified/checked 但 true/false 没有返回到 spec 来检查 .toBe(true )
页面对象:函数
checkOptionsEnabled: function(){
var status = false;
element.all(by.repeater('item in items')).then(function(options) {
options.forEach(function(option) {
option.getAttribute("aria-label").then(function(ariaLabel) {
expect(ariaLabel).toContain('enabled');
});
});
});
return status;
},
规范:调用函数
page.journeyModes.checkAll.click();
expect(page.journeyModes.checkOptionsEnabled()).toBe(true);
page.journeyModes.uncheckAll.click();
expect(page.journeyModes.checkOptionsDisabled()).toBe(true);
错误
1) TravelInfo As a user I can Select Journey Modes
Message:
Expected false to be true.
Stacktrace:
Error: Expected false to be true.
at new jasmine.ExpectationResult
C:\Users\sunil.sharma\Documents\PortalAutomatedTests\node_modules\protractor\n de_modules\minijasminenode\lib\jasmine-1.3.1.js:137:32) 在 [对象对象].
你需要的是reduce()
:
checkOptionsEnabled: function() {
return element.all(by.repeater('item in items')).reduce(function(acc, elem) {
return elem.getAttribute("aria-label").then(function (attr) {
return acc && (attr.indexOf("enabled") >= 0);
});
}, true);
},
这基本上是检查 aria-label
属性中是否存在 enabled
并累积结果布尔值。