Protractor : Read Table contents

JDunn picture JDunn · Apr 7, 2015 · Viewed 27.7k times · Source

I've been writing e2e tests for my angular js app and am unable to figure this out. I've got a table with data in it. I want to extract the first rows data.

<table>
    <tr>
        <td><\td>
        <td><\td>
        <td><\td>
    </tr>
</table>

I did this in protractors elementExplorer and it prints out the values of all 3 columns

element.all(by.repeater('item in items.list')).get(0).getText()
James
Byrne
1

If I do this, it prints out the first column value

element.all(by.repeater('item in items.list')).get(0).element(by.css('td')).getText()
WARNING - more than one element found for locator By.cssSelector("td") - the first result will be used
James

My Question is, how do I get the values of the other columns?

Answer

alecxe picture alecxe · Apr 8, 2015

Use all() in conjunction with map():

var row = element.all(by.repeater('item in items.list')).first();
var cells = row.all(by.tagName('td'));

var cellTexts = cells.map(function (elm) {
    return elm.getText();
});

Then, you can assert it to be an array of column texts:

expect(cellTexts).toEqual(["The first text", "The second text", "The third text"]);