protractorprotractor-net

How to get the count of a cell in a data using Protractor


Im unable to fetch the count of a cell of the below mentioned table. I want to fetch the count only in the table body.Please help me to achieve this- Im looking to get the count of-

<td class="ng-binding ng-scope" ng-if="paymentDetails.responseObject[0].fee_amount > 0">$0.90</td>

Please let me know how to achieve this. Thanks!!

<div class="show-for-large-up">
  <table>
    <thead>
      <tr>
        <!-- ngIf: paymentDetails.responseObject[0].fee_amount > 0 -->
        <th class="ng-binding ng-scope" ng-if="paymentDetails.responseObject[0].fee_amount > 0" ng-bind-html="receiptTemplate.paymentFee" width="300">Payment Fee</th>
        <!-- end ngIf: paymentDetails.responseObject[0].fee_amount > 0 -->
      </tr>
    </thead>
    <tbody>
      <!-- ngRepeat: lineItem in paymentDetails.responseObject[0].schedules[0].schedule_line_items track by $index -->
      <tr class="ng-scope" ng-repeat="lineItem in paymentDetails.responseObject[0].schedules[0].schedule_line_items track by $index">
        <td class="ng-binding">$45.00</td>
        <!-- ngIf: paymentDetails.responseObject[0].fee_amount > 0 -->
        <td class="ng-binding ng-scope" ng-if="paymentDetails.responseObject[0].fee_amount > 0">$0.90</td>
        <!-- end ngIf: paymentDetails.responseObject[0].fee_amount > 0 -->
      </tr>
      <!-- end ngRepeat: lineItem in paymentDetails.responseObject[0].schedules[0].schedule_line_items track by $index -->
      <tr class="ng-scope" ng-repeat="lineItem in paymentDetails.responseObject[0].schedules[0].schedule_line_items track by $index">
        <td class="ng-binding">Jul 21, 2017</td>
        <td class="ng-binding">$0.00</td>
        <!-- ngIf: paymentDetails.responseObject[0].fee_amount > 0 -->
        <td class="ng-binding ng-scope" ng-if="paymentDetails.responseObject[0].fee_amount > 0">$1.68</td>
        <!-- end ngIf: paymentDetails.responseObject[0].fee_amount > 0 -->
      </tr>
    </tbody>
  </table>
</div>

What i tried- But im get the count as 3. Actually the count should be 1

var paymentTable = $$('.show-for-large-up tr');
var row = paymentTable.$$('[ng-repeat="lineItem in paymentDetails.responseObject[0].schedules[0].schedule_line_items track by $index"]');
var cell = row.$$('[ng-if="paymentDetails.responseObject[0].fee_amount > 0"]');
var cellCount = cell.filter(function(elm) {
  return elm.count();
expect(elm.count()).toBe(1);
});

See image below Looking to fetch the count of rows for the column Payment fee


Solution

  • A filter will not work because a filter will get the first matching element and doesn't count, see also the docs

    You could achieve this with a each. You iterate though each matched element and then do the things you need. You code would look something like this

    var count = 0;
    
    // Get all the td's
    $$('[ng-if="paymentDetails.responseObject[0].fee_amount > 0"]')
      // Walk through all the found td's
      .each((element) => {
        // Get the text of the td
        return element.getText()
          .then((text) => {
            // Add a + 1 if the text matches
            if (text === '$0.90') {
              count + 1;
            }
          });
      });
    
    // Do the assertion
    expect(count).toBe(1);