2

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'
glepretre
  • 8,154
  • 5
  • 43
  • 57
Liad Livnat
  • 7,435
  • 16
  • 60
  • 98
  • If you are getting errors it's good practice to include them in your question. The more detail, the better – Tim Apr 02 '14 at 09:08
  • Why not using `var rows = element.all(by.repeater("item in publishers_data"));` then `sum = rows.count();`? – glepretre Apr 02 '14 at 16:32
  • i'm not looking for count i'm looking for sum of values... – Liad Livnat Apr 03 '14 at 14:54
  • oh, sorry for the misunderstanding – glepretre Apr 17 '14 at 08:38
  • 1
    @glepretre He wants to sum a value from each row (property a from item), so the topic is missleading ;-). But I was on that thinking like you, too. Liad should change topic to sum values from rows or something like that. – Sebastian Aug 12 '14 at 12:28

1 Answers1

5

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);
fer
  • 114
  • 2