isian8814
isian8814

Reputation: 191

return sum of count from element.all protractor

I need to calculate the sum of count of all elements matched the filter.

Here is the function:

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;
};

Here is how I use it:

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

Via using callback, I am able to return the sum, however, since I use element.forEach. the output like below, but I only need the final one, that is the last 2.

Started

0 0 0 0 0 1 1 1 2 2

Upvotes: 2

Views: 905

Answers (1)

alecxe
alecxe

Reputation: 473833

Use map() instead:

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);
    });
};

Usage:

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

Or:

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

Upvotes: 1

Related Questions