1

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!!!!

Alexandre
  • 432
  • 1
  • 3
  • 14

2 Answers2

1

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.

Kacper
  • 1,201
  • 1
  • 9
  • 21
  • hi, thanks for the answer, yes that was one of the problems, I edited the answer and the code looks cleaner now, the problem that I still have is to correctly sum the values, I can retrieve all values from the column (not sure if in the best way or not) but I cannot sum them, already tried the parseInt since Im getting strings but still with NaN error or ManagedPromise::13803 {[[PromiseStatus]] – Alexandre Aug 06 '18 at 11:16
  • Because you just define `var tempValue`, not init it. so the value of `tempValue` is `undefined`, then `Number(tempValue)` is `Number(undefined)` which return NaN. And `NaN + Number(..)` still return NaN – yong Aug 07 '18 at 05:15
  • Hi @Kacper thank you very much! worked perfectly, just had to change the column number being captured, have a good day! – Alexandre Aug 07 '18 at 05:47
  • Hi @yong Even doing that was giving me an error, I was not doing it properly but kacper's answer solved my problem, thanks! – Alexandre Aug 07 '18 at 05:47
1

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);
yong
  • 13,357
  • 1
  • 16
  • 27