AngularJS + Protractor sum all row values in repeater

試著忘記壹切 提交于 2019-12-05 05:00:02

问题


I'm testing AngularJS with Protractor, I have a repeater and I'm trying to sum all values in the rows, and compare it to the summary line value.

Here's my HTML:

<table>
  <th>
    <td>100</td>
  </th>
  <tr data-ng-repeat="item in publishers_data">
    <td>{{item.a}}</td>
  </tr>
</table>

I used the following code in my e2e test:

var total = 100;
var sum = 0;
element.all(by.repeater("item in publishers_data")).then(
    function(rows){
        for(var i=0;i<rows.length;i++){
            sum + = rows(by.model("{{item.a}}").getText();
        }
    });
expect(sum).toEqual(total);

I'm getting various kind of errors, can someone advice what am I doing wrong here?

An example error I get:

There was a webdriver error: TypeError Object [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[objec
t Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Ob
ject],[object Object],[object Object],[object Object] has no method 'getText'

回答1:


rows is an array and it's being called like a function (the side effect is that getText() is being called over the entire array instead of the desired element)

Also getText()'s response should be handled with another callback.

var total = 100;
var sum = element.all(By.repeater('item in publishers_data')).map(function(row) {
  return row.getText();
}).then(function(arr) {
  return arr.reduce(function(a, b) {
    return Number(a) + Number(b);
  })
});
expect(sum).toEqual(total);


来源:https://stackoverflow.com/questions/22806379/angularjs-protractor-sum-all-row-values-in-repeater

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