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