量角器 - 在ng-repeat中获取子元素的文本

时间:2015-03-19 17:35:33

标签: javascript angularjs jasmine protractor

我正在使用Protractor测试角度页面。这个页面有一个由ng-repeat填充的表格,我想在该表格中添加文本。这是HTML代码:

<div class="data-group ng-scope" ng-repeat="group in tableData  | filter: filterText | orderBy: getValueToOrderByGroup() : sortingCriteria.descending" ng-show="filtered.length > 0">
<div class="data-row group-header">
    <div class="col-md-6">
        <i ng-click="group.hideRows = !group.hideRows" ng-class="{'rotate-down': group.hideRows}" class="fa fa-fw fa-chevron-right"></i>
        <strong class="text-capitalize ng-binding" ng-click="group.hideRows = !group.hideRows"> HEADER TEXT </strong>
    </div>
</div>

<div collapse="group.hideRows" class="collapse in" style="height: auto;">

    <div class="data-row child-row ng-scope" ng-repeat="thing in filtered = (group.data | filter: filterStuff | filter: filterText | orderBy: getValueToOrderByChild() : sortingCriteria.descending)">

        <div class="col-md-3 cell-data child-row-indent text-capitalize">
            <span class="clickable-element ng-binding">CHILD ONE</span>&nbsp;<br>
        </div>


        <div class="col-md-2 child-row-indent-responsive">
            <span class="visible-xs visible-sm">Status:&nbsp;</span><span class="ng-binding"> CHILD TWO </span>
        </div>           
    </div>
</div>

这些是我提取文本的陈述:

var headerText = element.all(by.repeater('group in tableData')).get(0).all(by.tagName('div')).get(0).element(by.css('div > strong')).getText();

var childOne = element.all(by.repeater('group in tableData')).get(0).all(by.tagName('div')).get(1).element(by.css('div > div:nth-child(1) > span').getText();

当我运行它时,它返回了所有函数的整个块,但不返回实际文本。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:4)

重要的是要理解WebdriverJSprotractor本身完全基于承诺的概念,因为它们具有异步性质。请参阅Promises and the Control Flow

换句话说,您案例中的getText() 会返回承诺。如果要查看实际值,则需要解决它:

headerText.then(function (text) {
    console.log(text);
});

请注意,您不需要解决expect()内的承诺 - 它知道如何在做出预期之前解决承诺(感谢jasminewd)。