从element.all量角器返回计数总和

时间:2016-04-09 17:15:02

标签: javascript angularjs jasmine protractor

我需要计算与过滤器匹配的所有元素的总数。

这是功能:

this.filterPoolRecordGrid = function (searchText, callback) {
    var defer = protractor.promise.defer();
    element.all(By.repeater("(rowRenderIndex, row) in rowContainer.renderedRows track by $index")).then(function (eles) {
        var countR = 0;
        eles.forEach(function (ele) {
            ele.all(By.repeater('(colRenderIndex, col) in colContainer.renderedColumns track by col.uid')).filter(function (ele) {
                return  ele.getText().then(function (text) {
                    return text.indexOf(searchText) > -1;
                })
            }).count().then(function (value) {
                countR += value;
                callback(countR);
            });
        });
    });

    defer.fulfill();
    return defer.promise;
};

以下是我如何使用它:

it('Test valid input into groups search box', function () {
            poolRecordsPage.filterPoolRecordGrid('two', function (count) {
                console.info(count);
            });

通过使用回调,我可以返回总和,因为我使用了element.forEach。输出如下,但我只需要最后一个,即最后一个。

Started

0 0 0 0 0 1 1 1 2 2

1 个答案:

答案 0 :(得分:1)

改为使用enter image description here

this.filterPoolRecordGrid = function (searchText) {
    return element.all(By.repeater("(rowRenderIndex, row) in rowContainer.renderedRows track by $index")).map(function (ele) {
        return ele.all(By.repeater('(colRenderIndex, col) in colContainer.renderedColumns track by col.uid')).filter(function (ele) {
            return  ele.getText().then(function (text) {
                return text.indexOf(searchText) > -1;
            })
        }).count();
    }).then(function (counts) {
        return counts.reduce(function(a, b) { return a + b; }, 0);
    });
};

用法:

poolRecordsPage.filterPoolRecordGrid('two').then(function (count) {
    console.log(count);
});

或者:

expect(poolRecordsPage.filterPoolRecordGrid('two')).toEqual(10);