AngularJS +量角器总和都在转发行值 [英] AngularJS + Protractor sum all row values in repeater

查看:93
本文介绍了AngularJS +量角器总和都在转发行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用量角器测试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>

我用下面的code在我的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?

这是典型的错误,我得到:

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'


推荐答案

行是一个数组,它被称为像功能(副作用是的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 +量角器总和都在转发行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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