I'm trying to do some protractor testing and promises are not being resolved within the for loop.
In my test case I want to find a particular node in a ng-repeat element.
Here is the code to find such a node:
var firstProfileNode = function(nodename, profile){
element.all(by.repeater('node in tree_nodes')).then(function(treeNodes){
for(var i=0; i<treeNodes.length; i++){
var node = treeNodes[i].element(by.css('.tree-dnd-handle'));
node.getText().then(function(text){
console.log(i+" : "+text);
if (profile){
var pattern = '^' +nodename+' \\(\\d+ devices\\)$';
var regx = new RegExp(pattern);
if(regx.test(text)){
console.log('found')
return node;
}
}else{
if(text === nodename){
return node;
}
}
});
}
});
};
var test = firstProfileNode('ISR', true);
Here is the console output:
23 : edison (4 devices)
23 : ed21-mbr2
23 : ed22-mbr1
23 : ed22-mbr2
23 : ed21-mbr1
23 : c2800-12
23 : L1 (4 devices)
23 : c887VAM-1
23 : c891-1
23 : c887-1
23 : c3850-1
23 : ISR (3 devices)
found
23 : 3700 (2 devices)
23 : c3745-2
23 : c3745-1
23 : c2921-1
23 : c2800-11
23 : N7K (3 devices)
23 : n7k-2
23 : n7k-1
23 : n7k-3
23 : c2800-13
23 : c2800-14
The problem is that the getText() promise resolves after the for loop is done. Evidence of this is seen by the logged 'i' value which is 23, the final count. I'm looking for a way to have the for loop wait for the promise or another way to find the node altogether.
via xsdf
No comments:
Post a Comment