如何在量角器中的ng-repeat中添加/减去元素值

时间:2014-10-14 11:34:46

标签: protractor

我有以下HTML:

<div class="row" ng-show="isExpanded" ng-repeat="row in rows">
                <div class="colA">Account</div>
                <div class="colB">Kilometer</div>
                <div class="colA">Net</div>
                <div class="colC">61</div>
                <div class="colA">Vat</div>
                <div class="colC">V2</div>
                <div class="colA">Row Total</div>
                <div class="colC">76</div>
            </div>

<div class="row" ng-show="isExpanded" ng-repeat="row in rows">
                <div class="colA">Account</div>
                <div class="colB">Diners</div>
                <div class="colA">Net</div>
                <div class="colC">5</div>
                <div class="colA">Vat</div>
                <div class="colC">V2</div>
                <div class="colA">Row Total</div>
                <div class="colC">5</div>
            </div>

<div class="row" ng-show="isExpanded" ng-repeat="row in rows">
                <div class="colA">Account</div>
                <div class="colB">Repairs</div>
                <div class="colA">Net</div>
                <div class="colC">6</div>
                <div class="colA">Vat</div>
                <div class="colC">V2</div>
                <div class="colA">Row Total</div>
                <div class="colC">7</div>
            </div>

我希望在行的所有ng-repeat =行中添加Row total 我可以通过以下方式获取row_total值:

element.all(by.repeater('row in rows')).count().then(function(count)
{

    for(i = 0; i<count; i++)
    {

        row_total = element(by.repeater('row in rows').row(i)).then(function(elem)
        {
            row_elem = elem.element(by.css('div:nth-child(8)'));
            return row_elem.getText();

        });


    }

});

要执行的任务:找到添加所有这些值并返回总计

的方法

1 个答案:

答案 0 :(得分:1)

以下代码段应该适合您

element.all(by.repeater('row in rows'))
  .then(function (rows) { 
    var sum = 0; 
    for (i = 0; i < rows.length; i++) { 
      rows[i].element(by.css('div:nth-child(8)')).getText()
        .then(function (row_total) { 
          sum += parseFloat(row_total); 
        }); 
     } 
     expect(sum).toEqual(42); 
});

编辑:我更新了我的答案以获得更好的代码格式