18 January 2026:
Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.
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 ?
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
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
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.
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 ?
Please elaborate. Post the piece of code if you can. That will help understand it better.
Regards,
Pankaj.
Thanks for all
img262.imageshack.us/my.php?image=sahi1ll3.png
As you can see, the accessor is : " _image("Edit this record[1]") "
and my code is :
The goal is to manage to count how many devices are present in the array.
Thanks for help !
Try this if it works: Regards,
Pankaj Kumar.
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 am sorry...my mistake...Try this.
Please lemme know if it works for you.
Regards,
Pankaj.
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 ?
var i = 0;
while(_image("Edit this record["+i+"]") != null){
i++;
}
return i;
}
_assertEqual(20, how_many_devices());
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 ...
_assertEqual(14, how_many_devices()); Assertion Failed. Expected:[14] Actual:[0]
As if it doesn't manage to count ...
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;
}
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...
Thank you
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.
_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();
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.