Tim
Tim

Reputation: 203

Protractor Tests get Values of Table entries

I'm writing some protractor tests right now and got a little problem. How can I get the values of 'value1', 'value2' and 'value3' from the entry in the first row?

The HTML looks like this:

  <table>
  <tr data-ng-repeat="object in $data track by object.id">
    <td>{{object.value1}}
    </td>
    <td>
      {{object.value2}}
    </td>
    <td>
      {{object.value3}}
    </td>
  </tr>
</table>

Upvotes: 2

Views: 4019

Answers (2)

alecxe
alecxe

Reputation: 473823

First, you need to locate the desired table rows by the repeater:

var rows = element.all(by.repeater("object in $data"));

Then, to get to the cell texts, you may use repeater()'s row/column feature as Josh suggested or use map():

var data = rows.map(function (row) {
    var cells = row.all("td");
    return {
        value1: cells.first().getText(),
        value2: cells.get(1).getText(),
        value3: cells.get(2).getText()
    }
});

Then, the data would contain a list of row objects with values inside:

expect(data).toEqual([
    {value1: "test1", value2: "test2", value3: "test3"},
    {value1: "test4", value2: "test5", value3: "test6"}
]);

Upvotes: 1

Josh
Josh

Reputation: 44906

The protractor docs don't spell it out very clearly, but there is a .column() locator that applies to the by.repeater() locator.

Some examples from the site:

// Returns a promise that resolves to an array of WebElements from a column
var ages = element.all(
    by.repeater('cat in pets').column('cat.age'));

// Returns the H4 for the first book's name.
var firstBookName = element(by.repeater('book in library').
    row(0).column('book.name'));

Upvotes: 4

Related Questions