量角器:从 table 返回对象数组

Protractor: Returning an array of objects from a table

我有一个包含 3 列的 table 'firstname'、'lastname'、'birthday',像这样:

<table id="table">
    <tr>
        <td id="firstname">John</td>
        <td id="lastname">Smith</td>
        <td id="birthday">Jan 1 2014</td>
    <tr>
    <tr>
        <td id="firstname">Bill</td>
        <td id="lastname">Matthews</td>
        <td id="birthday">Jan 2 2014</td>
    <tr>
</table>

我想从这个 table 创建一个 JSON,像这样:

{
    firstname: "John",
    lastname:  "Smith",
    birthday:  "Jan 1 2014"
},
{
    firstname: "Bill",
    lastname:  "Matthews",
    birthday:  "Jan 2 2014"
}

我正在尝试这样的事情:

var tableRows = [];
element.all(by.tagName('tr')).each( function(element) {
    tableRows.push(
                     {
                         firstname: element(by.id('firstname')).getText(),
                         lastname:  element(by.id('lastname')).getText(),
                         birthday:  element(by.id('lastname')).getText()
                     }
                  );
});

使用map(),引用更新日志:

Added a map function to element.all to apply a function to each element and return the result of the transformation.

Resolve promises if there is an object that contains multiple promises. Added index as a second argument to the map function callback.

这里的关键点是它会解决多个承诺:

var items = element.all(by.tagName('tr')).map(function (tr) {
    return {
               firstname: tr.element(by.id('firstname')).getText(),
               lastname: tr.element(by.id('lastname')).getText(),
               birthday: tr.element(by.id('birthday')).getText()
           };
});