Shreyas Murali
Shreyas Murali

Reputation: 388

Table row count in protractor returns one always

<table class="customGridTable" tabindex="0" xpath="1"> <thead> <tr>  <th class="customGridHeader selectionHeader"> <input type="checkbox" class="selectAllItems"> </th> <th class="customGridHeader subgridExpandHeader nonResizable"> &nbsp; </th> <th class="customGridHeader editHeader nonResizable"> Edit </th>  <th class="customGridHeader typeHeader nonResizable"> Type </th>  <th class="customGridHeader customGridSortable" style="width: 25%" data-columnid="title"> <div> <span>Title</span>  <span class="columnEdit ui-button-icon-primary ui-icon ui-icon-pencil"></span>  </div> </th>  <th class="customGridHeader customGridSortable" style="width: 25%" data-columnid="created"> <div> <span>Created On</span>  </div> </th>  <th class="customGridHeader customGridSortable" style="width: 25%" data-columnid="modified"> <div> <span>Modified On</span>  </div> </th>  <th class="customGridHeader customGridSortable" style="width: 25%" data-columnid="keywords"> <div> <span>Keywords</span>  <span class="columnEdit ui-button-icon-primary ui-icon ui-icon-pencil"></span>  </div> </th>  <th class="fillHeader"></th></tr> </thead> <tbody>  <tr class="SearchResultItemView customGridHighlight"><td class="customGridDataCell AlignCenter">  <input type="checkbox" class="selectedItem" title="Item 1" checked="checked"></td><td class="customGridDataCell">  </td><td data-cellid="title" class="customGridDataCell customGridEditCell ">  <div class="valueContainer"> <span>Webinar_+Integrating+Your+Test+Automation+with+Jira</span> </div> </td><td data-cellid="created" class="customGridDataCell customGridEditCell ">  <div class="valueContainer"> <span>02/08/2019 12:12 p. m.</span> </div> </td><td data-cellid="modified" class="customGridDataCell customGridEditCell ">  <div class="valueContainer"> <span>25/11/2019 03:25 p. m.</span> </div> </td><td data-cellid="keywords" class="customGridDataCell customGridEditCell ">  <div class="valueContainer"> <span></span> </div> </td></tr><tr class="SearchResultItemView"><td class="customGridDataCell AlignCenter">  <input type="checkbox" class="selectedItem"></td><td class="customGridDataCell">   <span class="subgridExpand ui-icon ui-icon-triangle-1-e"></span> </td><td class="customGridDataCell">   <span class="editRow ui-icon ui-icon-pencil"></span> </td><td class="customGridDataCell">  <div class="item_type_icon document_icon pdf"></div></td><td data-cellid="title" class="customGridDataCell customGridEditCell ">  <div class="valueContainer"> <span>US Exp Report</span> </div> </td><td data-cellid="created" class="customGridDataCell customGridEditCell ">  <div class="valueContainer"> <span>20/11/2019 06:08 p. m.</span> </div> </td><td data-cellid="modified" class="customGridDataCell customGridEditCell ">  <div class="valueContainer"> <span>26/11/2019 10:32 a. m.</span> </div> </td><td data-cellid="keywords" class="customGridDataCell customGridEditCell ">  <div class="valueContainer"> <span>EditedAgain</span> </div> </td></tr><tr class="SearchResultItemView"><td class="customGridDataCell AlignCenter">  <input type="checkbox" class="selectedItem"></td><td class="customGridDataCell">   <span class="subgridExpand ui-icon ui-icon-triangle-1-e"></span> </td><td class="customGridDataCell">   <span class="editRow ui-icon ui-icon-pencil"></span> </td><td class="customGridDataCell">  <div class="item_type_icon document_icon pdf"></div></td><td data-cellid="title" class="customGridDataCell customGridEditCell ">  <div class="valueContainer"> <span>US Exp Report</span> </div> </td><td data-cellid="created" class="customGridDataCell customGridEditCell ">  <div class="valueContainer"> <span>20/11/2019 06:11 p. m.</span> </div> </td><td data-cellid="modified" class="customGridDataCell customGridEditCell ">  <div class="valueContainer"> <span>26/11/2019 10:34 a. m.</span> </div> </td><td data-cellid="keywords" class="customGridDataCell customGridEditCell ">  <div class="valueContainer"> <span>EditedAgain</span> </div> </td></tr><tr class="SearchResultItemView"><td class="customGridDataCell AlignCenter">  <input type="checkbox" class="selectedItem"></td><td class="customGridDataCell">  </td><td class="customGridDataCell">   <span class="editRow ui-icon ui-icon-pencil"></span> </td>
<td data-cellid="keywords" class="customGridDataCell customGridEditCell ">  <div class="valueContainer"> <span></span> </div> </td></tr><tr class="SearchResultItemView"><td class="customGridDataCell AlignCenter">  <input type="checkbox" class="selectedItem"></td><td class="customGridDataCell">  </td><td class="customGridDataCell">   <span class="editRow ui-icon ui-icon-pencil"></span> </td><td class="customGridDataCell">  <div class="item_type_icon document_icon pdf"></div></td><td data-cellid="title" class="customGridDataCell customGridEditCell ">  <div class="valueContainer"> <span>todaysssss</span> </div> </td><td data-cellid="created" class="customGridDataCell customGridEditCell ">  <div class="valueContainer"> <span>28/08/2019 02:42 p. m.</span> </div> </td><td data-cellid="modified" class="customGridDataCell customGridEditCell ">  <div class="valueContainer"> <span>20/09/2019 02:54 p. m.</span> </div> </td><td data-cellid="keywords" class="customGridDataCell customGridEditCell ">  <div class="valueContainer"> <span>sample</span> </div> </td></tr><tr class="SearchResultItemView"><td class="customGridDataCell AlignCenter">  <input type="checkbox" class="selectedItem"></td></tr></tbody>  </table>

    async getRows(): Promise<ElementFinder[]> {
    return new Promise(async (resolve, reject) => {
      this.waitForPresence();
      await browser.sleep(this.gridTimeout);
      resolve(this.component.element(by.css("tbody")).all(by.css("tr")));
    });
  }
    async getRowCount() {
    let retVal = 0;
    await this.getRows().then(async rws => {
      retVal = rws.length;
    });
    return retVal;
  }

I have the above code to get row count, but I get 1 as the result all the time.

Upvotes: 0

Views: 52

Answers (2)

yong
yong

Reputation: 13712

getRows() return value type should be ElementArrayFinder

async getRows(): ElementArrayFinder {
  await this.waitForPresence();
  await browser.sleep(this.gridTimeout);
  return this.component.all(by.css("tbody > tr"));
}

async getRowCount() {
  return await this.getRows().count()
}

Upvotes: 1

Joaquin Casco
Joaquin Casco

Reputation: 734

Why don't use the .count() method that protractor gives you?
It looks like you want to count all the tr's iside the <tbody> ? In that case, try the following:

const tbody = element(by.css('tbody'));
const tbodyRows = tbody.all(by.css('tr'));

const getRowCount = async () => {
    let rowsCount = await tbodyRows.count();
    return rowsCount;
}

Upvotes: 0

Related Questions