Alexandre
Alexandre

Reputation: 462

Protractor - How to jump to the next row in a grid and return the found value

Need a quick help with Protractor please.

I need to get the first cell text which is different from 0, then get from this row data from column 0 and column 4

In this I am able to get the first value from column 4 correctly

var resultsTableSummary = element(by.id('lvTable')); //Results table
var rows = resultsTableSummary.all(by.tagName("tr")); // get rows 
var cells = rows.all(by.tagName("td")); // get cell values

  cells.get(4).getText().then(function(result) 
  {
    if (result > 0) 
    {           
      console.log('for Result value '+ result);
    } 
  }) 

but I would like to know 2 things:

1 - How can I move to the next tr (row) in case the first row has no value? I tried to create an array with the elements but I was not so successful, any ideas please?

2 - How can I return values from this promise? the function is getting the value from the column number 4 and and displaying in the screen for me but I want to use it in the next steps of my code

Thank you!!

Upvotes: 0

Views: 321

Answers (1)

Silvan Bregy
Silvan Bregy

Reputation: 2734

I would recommend to just keep the row elements and filter them to your needs. Then you would always have access to every row since you are always processing row per row. As follows:

getFirstValidRowData(rows: ElementArrayFinder): promise.Promise<string[]> {
    return rows.filter(function (row) {
      // filter rows by its values. IF one value is greater than 0 row is valid
      return row.all(by.tagName('td')).filter(function (cell) {
      // filter a row by valid cells
        return cell.getText().then(function (text) {
          return parseInt(text) > 0;
        });
      // if there are cells found row is valid
      }).then(function (cells) {
        return cells.length > 0;
      });
      // take valid row and get cells 1 and 4
    }).first().all(by.css('td')).filter(function (cell, index) {
      return index === 0 || index === 3;
      // return values of these cells
    }).map(function (cell) {
      return cell.getText();
    });
  }

In test you can then do something like:

it('should have one valid row', async function() {
  var resultsTableSummary = element(by.id('lvTable')); //Results table
  var rows = resultsTableSummary.all(by.tagName("tr"));
  var validRowsData = await getFirstValidRowData(rows);
  // do whatever you want with your data from cell 1-4
});

I did not test this but syntactically it's correct. This should match to your needs.

Upvotes: 1

Related Questions