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.

How do you count rows of an array ?

lepierrotlepierrot Members
edited November -1 in Sahi - Open Source
Hi,

I'm developping tests for a portal. One of the pages i'm working on displays an array with the number of elements it contains.
For example, I have a switch an many pc's connected on. Each time I add a new pc, the array has one more row. But let's think about this following case: a user that have a lot of pc's want to count them. That's why I'd like to know how can I count it.

When many devices are added, their common element is an image in the first column : _image("Edit this record") for the first one, _image("Edit this record[2]") for the second etc...

I thought I could make a "while" this way:

$i=0;
while(_assertNotNull(_image("Edit this record[$i]"))){
$i++;
}


In fact: while my row exist, I increment the $i variable. And in the end, $i represents the number of devices because the loop will stop when "Edit this record[$i]" no longer exists (so it's the last device connected) ...
But it doesn't work and sahi directly fails...I think it's because "_assertNotNull" doesn't send back any value, not even a "true" or a "false", so there is no condition in the "while" loop.

An idea ?

Thanks a lot

Comments

  • Hi,

    Try this if it works.
    Suppose your array name is $arr.
    Count the length as:
    $arr.length;
    It will return the number of rows your array has.

    Regards,
    Pankaj.
  • Thank you for your answer.
    I'll try it on monday but I need a precision first:

    The problem is that the name of my table is weird : when I check a cell with "Ctrl" with sahi, Sahi tells me that the name of the cell is _table(cell(0,5) for example (I don't rememmber exactly!). I don't know what is it and my array seems to be not set as a "$array" variable. So what is the name of my array ?
  • Hi,
    Please elaborate. Post the piece of code if you can. That will help understand it better.

    Regards,
    Pankaj.
  • Ok I will do this tomorrow when I'll be at my job !
    Thanks for all
  • Hi, the array which I want to count the rows of is the following:

    img262.imageshack.us/my.php?image=sahi1ll3.png

    As you can see, the accessor is : " _image("Edit this record[1]") "

    and my code is :
    function how_many_devices(){
    
            while(_assertNotNull(_image("Edit this record[$i]"))){
                $i++;    
            }
            
    }
    
    how_many_devices();
    
    The goal is to manage to count how many devices are present in the array.

    Thanks for help !
  • pankaj.nithpankaj.nith Members
    edited June 2008
    Hi,

    Try this if it works:
    function how_many_devices(){
      var $i=0;
      while(_condition(_image("Edit this record[$i]") != null)){
                $i++;    
       }
      return $i;
    }
    
    var $rows = how_many_devices();
    
    Regards,
    Pankaj Kumar.
  • Hi pankaj, thanks for helping me :)

    I've ran your script and sahi always show me the same message:

    saveCondition(_image("Edit this record[0]") != null);
    saveCondition(_image("Edit this record[0]") != null);
    saveCondition(_image("Edit this record[0]") != null);
    saveCondition(_image("Edit this record[0]") != null);
    saveCondition(_image("Edit this record[0]") != null);
    saveCondition(_image("Edit this record[0]") != null);
    ...

    It seems it does not go forward after the first test for "row 0"...
  • I've replaced "null" by "undefined" but it's the same :'(
  • Hi,

    I am sorry...my mistake...Try this.
    function how_many_devices(){
      var $i=0;
      while(_condition(_image("Edit this record[" + $i + "]") != null)){
                $i++;    
       }
      return $i;
    }
    
    var $rows = how_many_devices();
    
    Please lemme know if it works for you.

    Regards,
    Pankaj.
  • lepierrotlepierrot Members
    edited June 2008
    One more time thank you for your help!

    hum it's doing the same as before, but with the new syntax !

    saveCondition(_image("Edit this record[" + 0 + "]") != null);
    saveCondition(_image("Edit this record[" + 0 + "]") != null);
    saveCondition(_image("Edit this record[" + 0 + "]") != null);
    saveCondition(_image("Edit this record[" + 0 + "]") != null);
    saveCondition(_image("Edit this record[" + 0 + "]") != null);
    saveCondition(_image("Edit this record[" + 0 + "]") != null);

    What is saveCondition ?
  • narayannarayan Administrators
    function how_many_devices(){
    var i = 0;
    while(_image("Edit this record["+i+"]") != null){
    i++;
    }
    return i;
    }

    _assertEqual(20, how_many_devices());
  • Thank you :)

    But still does not work ... :(
    I don't understand, I've created some much more complicated functions that work perfectly, but this one doesn't want to ...
  • narayannarayan Administrators
    What is the failure?
  • The failure is :

    _assertEqual(14, how_many_devices()); Assertion Failed. Expected:[14] Actual:[0]

    As if it doesn't manage to count ...
  • no more ideas ...? :(
  • tihotiho Members
    It is obvious that this can't work.
    You have to declare a distinct variable for counting. If "Edit this record[0]" is null then variable i will never get incremented.
    Try to use the following snippet:

    function how_many_devices(){
    var i = 0;
    var count = 0;
    while(_image("Edit this record["+i+"]") != null){
    count++;
    }
    return count;
    }
  • Thank you for your answer, but it still doesn't work :

    There is an error telling me that i is not defined, so I've replaced i by $i everywhere, and then the error is the following :

    saveCondition(_image("Edit this record["+0+"]") != null);
    saveCondition(_image("Edit this record["+0+"]") != null);
    saveCondition(_image("Edit this record["+0+"]") != null);
    saveCondition(_image("Edit this record["+0+"]") != null);

    Same as before...
  • tihotiho Members
    Could you please post your code snippet?
  • function how_many_devices(){
       var i = 0;
       var count = 0;
       while(_condition(_image("Edit this record["+i+"]") != null)){
           count++;   
       }
       return count;
    }
    
    var n = how_many_devices();
    
    if(_condition(n>=10))
            _click(_link("Sim Array"));
    
    Here it is :)
    Thank you
  • Hi,

    You are using count and i in the loop but your i value is not changing.
    while(_condition(_image("Edit this record["+i+"]") != null)){
    count++;
    }
    Try:
    while(_condition(_image("Edit this record["+count+"]") != null)){
    count++;
    }
    or increment i also as it may be the case.
    Also try using $n in place of n as it is not inside any schedular function I suppose.

    Regards,
    Pankaj.
  • tihotiho Members
    Ohh... I almost lost my nerves.. I have experienced the same failure. First I couldn't figure out what happens.. but then I took a look at the generated java script:

    _sahi.scriptScope = function (){
    _sahi.scriptScope.execute = function(s){eval(s);};

    //Your code starts

    function how_many_devices(){
    var i = 0;
    var count = 0;
    while (true) {
    _sahi.schedule("_sahi.saveCondition(_sahi._image(\"Edit this record[\"+i+\"]\") != null);", "C:\\Program Files\\sahi\\bin\\..\\scripts\\creatingClicks.sah&n=4");
    if ("true" != _sahi._getGlobal("condn" + (_sahi.cmds.length))) break;//){count++;
    }
    return count;
    }

    As you can see the increment operation is commented out. This seems like a bug in the Sahi compiler. I also found a workaround:
    Put an empty comment in the line before the increment.
    This should possibly work:

    function how_many_devices(){
    var $i=0;
    while(_condition(_image("Edit this record[" + $i + "]") != null)){
    //
    $i++;
    }
    return $i;
    }

    var $rows = how_many_devices();
  • lepierrotlepierrot Members
    edited June 2008
    oh my god you are such a genius ... it works ...
    Thank you to pankaj, narayan and of course you for having answered to my askings...
    I won't hesitate to ask you again... :d

    I couldn't find this alone... really thank you.
Sign In or Register to comment.