AngularJS + Protractor 将转发器中的所有行值相加 [英] AngularJS + Protractor sum all row values in repeater

查看:13
本文介绍了AngularJS + Protractor 将转发器中的所有行值相加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Protractor 测试 AngularJS,我有一个中继器,我正在尝试对行中的所有值求和,并将其与汇总行值进行比较.

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.

这是我的 HTML:

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

我在 e2e 测试中使用了以下代码:

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?

我得到的示例错误:

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'

推荐答案

rows 是一个数组,它像函数一样被调用(副作用是 getText() 在整个数组而不是所需元素上被调用)

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)

getText() 的响应也应该用另一个回调来处理.

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

这篇关于AngularJS + Protractor 将转发器中的所有行值相加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆