18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

is there way to get cell count?

sumeetsumeet Members
edited November -1 in Sahi - Open Source
A table has multiple rows for the column "Status" which holds "success" "failure" "warning" etc. I needed to get count of the cell for each type of status. Any idea?

Comments

  • narayannarayan Administrators
    Easiest would be to write a browser function
    <browser>
    function getCount(status){
      var count = 0;
      var colIndexOfStatus = 6; 
      var table = _table("id");
      for (var i=0; i<table.rows.length; i++){
         if (_getText(table.rows[i].cells[colIndexOfStatus]) == status) count++;
      }
      return count;
    }
    </browser>
    
    var $countSuccess = 0;
    _set($countSuccess, getCount("success"));
    _alert($countSuccess);
    
  • sumeetsumeet Members
    Yes, that is what I did but have that function stored in common functions sahi script. But I thought if JS or Sahi provides some kind of API, as it will be great help to do data compare.
  • Wish I'd found this earlier... would have saved some time :) Still, I came up with an alternate solution that might not be as foolproof but could be used for any text on a page using outerHTML (though you could use inner as well if you don't want the tags)...

    // count the occurrences of $string on a page. Pass sahi objects outerHTML attribute
    function countStrings($string) {
    var $count = 0;
    _set($page, document.body.outerHTML);
    _set($count, $page.split($string).length -1);
    return($count);
    }

    $count = countStrings("<TD>unit </TD>");
    _alert($count);
  • Woops... of course to replace my call with:

    _set($count, getTemplateCount($template));
    _alert($count);
Sign In or Register to comment.