简体   繁体   English

如何在for循环中处理量角器承诺?

[英]How to handle protractor promises within a for loop?

I'm trying to do some protractor testing and promises are not being resolved within the for loop. 我正在尝试做一些量角器测试,并且在for循环中没有解决承诺。

In my test case I want to find a particular node in a ng-repeat element. 在我的测试用例中,我想在ng-repeat元素中找到一个特定的节点。

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. 问题是getText()promise在for循环完成后解析。 Evidence of this is seen by the logged 'i' value which is 23, the final count. 记录的'i'值是最终计数23,可以看出这一点。 I'm looking for a way to have the for loop wait for the promise or another way to find the node altogether. 我正在寻找一种方法让for循环等待承诺或另一种方式来完全找到节点。

You are expecting the code to be executed from top to bottom synchronously, but it is actually asynchronous - the loop would be over at the moment first getText() is resolved. 您希望代码可以从上到下同步执行,但它实际上是异步的 - 在第一次getText()解析时,循环将结束。

I think what you need is a filter() : 我认为你需要的是一个filter()

var firstProfileNode = function(nodename, profile) { 
    return element.all(by.repeater('node in tree_nodes')).filter(function(treeNode) {
        return treeNode.element(by.css('.tree-dnd-handle')).getText().then(function(text) {
            if (profile) {
                var pattern = '^' +nodename+' \\(\\d+ devices\\)$';
                var regx = new RegExp(pattern);
                return regx.test(text);
            } else {
                return text === nodename;
            }
        });
    }).first();
};

The firstProfileNode() function would return an ElementFinder instance corresponding to the desired filtered node element. firstProfileNode()函数将返回与所需过滤node元素对应的ElementFinder实例。

This is a pretty common occurrence. 这是很常见的事情。

The promises resolve asynch AFTER the whole for loop has been run and therefore only the last node is being returned (and i is always 23) 承诺在整个for循环运行后解析异步,因此只返回最后一个节点( i总是23)

A common approach is to enclose the callback (ie) 一种常见的方法是封装回调(即)

function fooBar(node, i) {
        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;
                }
            }
        });    
}

In a separate method, so that every node and i variable is closed in scope in that method and doesn't change. 在一个单独的方法中,每个nodei变量在该方法的closed in scope并且不会更改。 Then simply call the method. 然后简单地调用该方法。

You can try something like this: 你可以尝试这样的事情:

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'));
            getNodeText(i, node);
        }
    });
  };

var getNodeText = function(i, node) {
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);

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

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