首页 > 解决方案 > 关于量角器中 then 函数的参数。在下面的代码中,什么将传递给“值”?

问题描述

如果 bar 等于 jsonObj.alias 它应该返回 true 或 false 并且这应该是 next then 函数正确的参数。但这不是在这里发生的。请清除这一点。最后一个 then 函数的参数是什么以及如何?

element
    .all(by.repeater('portGroup in displayedCollection'))

    .filter(function(eachRow) {
        return eachRow.element(by.css('td:nth-child(2)')).getText().then(function(bar){
            return bar === jsonObj.alias;
        });
    })

    .then(function(values){
        values[0].element(by.css('span[class="ng-binding"]')).click();
    }); 

标签: javascriptprotractor

解决方案


将你的代码分成两部分可以让你理解你的问题。

var satisfiedRows = element
    .all(by.repeater('portGroup in displayedCollection'))

    .filter(function(eachRow) {
        // the filter function is to find out from all rows 
        // which's 3rd cell text equal to `jsonObj.alias`
        return eachRow.element(by.css('td:nth-child(2)')).getText().then(function(bar){
            return bar === jsonObj.alias;
        });
    });  // we can split your code at here


    // satisfiedRows is a promise which eventual value are satisfied rows
    // you call `then()` on this promise, the argument `function()` will be 
    // passed-in the promise's eventual value. In your case, 
    // they are rows which's 3rd cell text equal to `jsonObj.alias`
    satisfiedRows.then(function(rowsAfterFilter){
        rowsAfterFilter[0].element(by.css('span[class="ng-binding"]')).click();
    });

推荐阅读