angularjs - Protractor :从表中返回对象数组

标签 angularjs testing protractor end-to-end

我有一个包含 3 列“名字”、“姓氏”、“生日”的表格,如下所示:

<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>

我想从这个表创建一个 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.

这里的关键点是它会解析多个 promises:

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()
           };
});

关于angularjs - Protractor :从表中返回对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27853926/

相关文章:

javascript - 使用 Angular、JavaScript 编辑和删除表单

angularjs - 什么是 ng-transinclude?

java - 我如何对 toCompare() 方法进行单元测试,比较 2 个对象?

selenium - Protractor : Error while waiting for Protractor to sync with the page: "Could not find testability for element."

angularjs - 从 Protractor 中的行元素获取索引

javascript - ngModel、点规则和多级范围层次结构

javascript - form.$error.required 和 form.$invalid 的冲突验证

c# - 如何在我上传文件的 Asp.Net 核心 Web API 端点上进行集成测试?

java - Android 上的压力测试内存

selenium - Chrome : fake microphone input for test purpose