Kishan Patel
Kishan Patel

Reputation: 1383

How to count number of rows in a table in Protractor?

I want to find number of counts of row i.e. how many tr tags are present. What should i do to automate it for protractor ?

<table id="voyageGrid" class="ui-iggrid-table ui-widget-content" ng-reflect-id="voyageGrid" role="grid" aria-describedby="voyageGrid_container" style="table-layout: fixed; overflow: hidden;" cellspacing="0" cellpadding="0" border="0">
<colgroup>
<thead role="rowgroup">
<tbody class="ui-widget-content ui-iggrid-tablebody ui-ig-record ui-iggrid-record" role="rowgroup">
<tr role="row" tabindex="0">
<tr class="ui-ig-altrecord ui-iggrid-altrecord" role="row" tabindex="0">
<tr role="row" tabindex="0">
<tr class="ui-ig-altrecord ui-iggrid-altrecord" role="row" tabindex="0">
<tr role="row" tabindex="0">
<tr class="ui-ig-altrecord ui-iggrid-altrecord" role="row" tabindex="0">
<tr role="row" tabindex="0">
<tr class="ui-ig-altrecord ui-iggrid-altrecord" role="row" tabindex="0">
<tr role="row" tabindex="0">
<tr class="ui-ig-altrecord ui-iggrid-altrecord" role="row" tabindex="0">
</tbody>
<tfoot id="voyageGrid_footer_container" class="" role="rowgroup" style="display: none;"/>
</table>

I want to have a count of tr tags i.e. there are 10 tr tags so i want count = 10. How should i proceed? Did many things but none worked This is something I tried but didn't work

var row = element.all(by.xpath('.//*[@class="ui-widget-content ui-iggrid-tablebody ui-ig-record ui-iggrid-record"]'));
var value = row.all(by.tagName("tr"));
console.log(value.count());

Upvotes: 1

Views: 7899

Answers (1)

Sudharsan Selvaraj
Sudharsan Selvaraj

Reputation: 4832

Your code is almost correct. value.count() will not directly give you the count.It will return you a promise which needs to be resolved explicitly.Look at below example.

$(".voyageGrid tr").count().then(function(rowCount){
  console.log("Count:"+rowCount) 
})

Upvotes: 2

Related Questions