7

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?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
JDunn
  • 107
  • 1
  • 3
  • 10
  • Did you consider giving each column a `name`, then using `by.name()` to grab the specific column after you have the first row (from `.get(0)`)? – Aaron Apr 07 '15 at 21:56

2 Answers2

11

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"]);
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thanks alexcxe. That worked. How do I get individual elements of cellTexts. For example I want to extract "The second text". Also "The third text" happens to be the text of a button which I'd like to click. – JDunn Apr 07 '15 at 23:55
  • 1
    @JDunn you can use `get()`: `expect(cellTexts.get(1)).toEqual("The second text");`. If you need to locate the button in the third td: `cells.get(2).element(by.tagName("button")).click();`. Hope it helps. – alecxe Apr 07 '15 at 23:59
  • I get a Failed: undefined is not a function for cellTexts.get(1), the button clicking part worked well though – JDunn Apr 08 '15 at 00:59
  • @JDunn what about `expect(cellTexts[1]).toEqual("The second text");`? Thanks. – alecxe Apr 08 '15 at 01:07
  • Expected undefined to equal 'The second text' – JDunn Apr 08 '15 at 01:13
10

Easiest way would be as below:

var tabledata = element.all(by.css("./table"));

// get rows 
var rows = tabledata.all(by.tagName("tr"));

// get cell values
var cells = rows.all(by.tagName("td"));

expect(cells.get(0).getText()).toEqual("something")
expect(cells.get(1).getText()).toEqual("something")
expect(cells.get(2).getText()).toEqual("something")

I implemented it and it is working for me.

slartidan
  • 20,403
  • 15
  • 83
  • 131
Abbas Ali Husain
  • 195
  • 2
  • 13