量角器看不到元素的子元素

Protractor does not see the element's children

我有这个功能

var goThrough = function(opt_parentElement) {
    var using = opt_parentElement;
    var matches = [];
    var addMatchingLeaves = function(element) {
        if (element.children) {
            if (element.children.length === 0) {
                matches.push(element);
            }
            for (var i = 0; i < element.children.length; ++i) {
                addMatchingLeaves(element.children[i]);
            }
        }
    };
    addMatchingLeaves(using);

    return matches;
};

然后我在测试中称它为

amountOfElements = goThrough(element(by.css('.form-group')));

html本身就是

<div class="form-group has-feedback">
      <input id="password" name="password" type="password"> 
</div>

问题是函数看不到 .form-group 的子项。我试过其他元素,但结果是一样的,函数没有先进入 "if"

element(by.css('.form-group')) returns 一个 ElementFinder,而不是您需要查询其 children 的 DOM 元素。如果您有很多元素但可以完成工作,这会有点慢:

var getLeafs = function(node) {
    return node.all(by.css('*')).filter(function(child) {
        return child.all(by.xpath('*')).count().then(function(childrenCount) {
            return childrenCount === 0;
        });
    });
};

returnsnode上下文中的元素没有children.

用法:

var leafs = getLeafs(element(by.css('.form-group')));

leafs.count().then(function(c) {
    console.log(c);
});