Alexandre
Alexandre

Reputation: 462

Protractor - How to get all cells from one column and sum them when the Grid has id="0-0" for rows-Columns

I am validating a drill down process in the portal i am testing, for this my script is doing: Read the value from the first row of a table and click at this value (there is a link for certain cells that perform the drill down to the detail page) To click at this particular cell I am using it's ID:

<table id="transHistTable" class="table table-hover  table-bordered table-striped dataTable no-footer" style="width: 100%" role="grid" aria-describedby="transHistTable_info">
<thead>
<tbody>
<tr role="row" class="odd">
<td id="0-0" class="ng-scope">31 Jul 2018</td>
<td id="0-1" class="ng-scope">RandomText0</td>
<td id="0-2" class="ng-scope">RandomText1</td>
<td id="0-3" class="ng-scope">EmptyValue</td>
<td id="0-4" class="ng-scope">Value I Click And Save it</td>

So for this table I am clicking directly to the row 0 column 4 since my data and my filters will always bring only one row, but then, comes my problem.... When the drill down is performed I never know how many rows I will have since it depends of user operations. I need to perform a validation to compare the sum of all the values from the table displayed after the drill down with the value captured from table "transHistTable" row 0 column 4

This is the values I get after performing the Drill Down:

<table id="transHistDetailTable" class="table table-hover  table-bordered table-striped dataTable no-footer" style="width: 100%" role="grid" aria-describedby="transHistDetailTable_info">
<thead>
</thead>
<tbody><tr role="row" class="odd">
<td id="0-0" class="ng-scope">Site</td>
<td id="0-1" class="ng-scope">Date</td>
<td id="0-2" class="ng-scope">Time</td>
<td id="0-3" class="ng-scope">I</td>
<td id="0-4" class="ng-scope">value 1</td>
<td id="0-5" class="ng-scope">value 2</td>
<td id="0-6" class="ng-scope">value 3</td>
<td id="0-7" class="ng-scope">12</td>
</tr></tbody>
</table>

So what I would like to do is reading all the rows (could be 0,1,2,3,4,5...) saving the value that is stored in Column 7 then after this is done, perform a sum and then comparing with the value I have saved from the first table.

My code is this one:

            var rowstransHistDetail = element(by.id('transHistDetailTable')).all(by.tagName("tr"));

            rowstransHistDetail.count().then(function(rcount){
                //In case only 1 row is displayed
                if (rcount < 3)
                {
                    element(by.id('0-7')).getText().then(function(valueQty){  
                        expect(valueQty).toEqual(600)
                    })
                }
                else
                {
                    var tempValue
                    for (i=0; i < rcount; i++)
                    {
                        element(by.id(i+'-7')).getText().then(function(valueQty){

                            tempValue = Number(tempValue) + Number(valueQty)
                        })
                    }

                    expect(tempValue).toEqual(600);
                }
            });

But when I execute this, gives me a undefined value

Any ideas how to solve this please?

Thank you!!!!

Upvotes: 1

Views: 1176

Answers (2)

yong
yong

Reputation: 13722

Two options for your issue:

1) using element.all().reduce()

let total = element
    .all(by.css('#transHistDetailTable > tbody > tr > td:nth-child(8)'))
    .reduce(function(acc, item){
        return item.getText().then(function(txt) {
            return acc + txt.trim() * 1;
        });
    }, 0);

expect(total).toEqual(600);

2) using element.all().getText() and Array.reduce

let total = element
    .all(by.css('#transHistDetailTable > tbody > tr > td:nth-child(8)'))
    .getText(function(txts){ //txts is a string Array
        return txts.reduce(function(acc, cur){
            return acc + cur * 1;
        }, 0);
    });

expect(total).toEqual(600);

Upvotes: 1

Kacper
Kacper

Reputation: 1199

It seems that you are incrementing a value in a loop before execution. See here: https://stackoverflow.com/a/6867907/6331748

Should bee i++ instead of ++i in a loop.

Drop me a line if I'm wrong.

===========================

Edited: Here's some code from my side:

var expectedCells = element.all(by.css('#transHistDetailTable tr td:nth-of-type(5)'));
var currentSum = 0;
expectedCells.each((eachCell) => {
    eachCell.getText().then((cellText) => {
        currentSum += Number(cellText);
    });
}).then(() => {
    expect(currentSum).toEqual(600);
});

Sorry, but wasn't able to test it. I only want to share a main idea and elaborate it.

expectedCells are all id=n-4 cells. We go through all elements and get text from them, change to the number type and add to current value. Aftermath we do an assertion.

It also looks that if statement is not necessarily. Let me know how it works.

Upvotes: 1

Related Questions