Protractor Tests get Values of Table entries

眉间皱痕 提交于 2019-12-30 11:17:10

问题


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>

回答1:


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'));



回答2:


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"}
]);


来源:https://stackoverflow.com/questions/34135713/protractor-tests-get-values-of-table-entries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!