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.

Trouble with a FOR loop

StringyLowStringyLow Members
edited November -1 in Sahi - Open Source
I have a script that won't continue past a certain point unless the FOR loop section is commented out.

Can you guys see anything that I might be missing?

I've copied what I think are the relevant parts below:



var $arrPicks = new Array();

<<<<< $arrPicks is loaded with a list of random numbers here >>>>>

// ##### Click checkboxes

var $len = $arrPicks.length; // cache the length so it isn't looked up every loop
for (var $j = 0; $j < $len; $j++) {
_checkbox("checkbox[" + $arrPicks[$j] + "]").click();
}

// ##### Navigate to Confirm changes page
_click(_submit(" Confirm changes \u00bb"));

<<<<< Script won't continue below this point >>>>>>

// ##### Verify the number of added keywords
_assertExists(_cell(_table(9), 1, 1));
_assertEqual("0", _getText(_cell(_table(9), 1, 1)));
_assertContainsText("0", _cell(_table(9), 1, 1));



Thanks for any ideas!

Comments

  • pankaj.nithpankaj.nith Members
    edited September 2008
    Hi Stringy,

    I am not sure but using variable index in the array might be posing the problem. Try wrapping the entire thing in a function and calling it.
    Try it.. will try using ur loop at my place. :)

    Regards,
    Pankaj.
  • Hi,

    Sorry i forgot to mention that directly assigning the length of the array to a variable won't work.
    Try using _set() for the purpose. Other alternatives are directly using the length like:
    for (var $j = 0; $j < $arrPicks.length; $j++) {
       _checkbox("checkbox[" + $arrPicks[$j] + "]").click();
    }
    
    or use a function that returns the length of the array.

    Regards,
    Pankaj.
  • I tried:

    var $len = $arrPicks.length;
    for (var $j = 0; $j < $len; $j++) {
    var $num = clickCheckboxes($j);
    _checkbox("checkedIds[" + $num + "]").click();
    }

    function clickCheckboxes($j) {
    return $arrPicks[$j];
    }

    and I tried

    var $item = 0;
    while($arrPicks.length > 0) {
    $item = $arrPicks.pop();
    _checkbox("checkedIds[" + $item + "]").click();
    }

    And I tried using $arrPicks.length.

    None of them worked.

    One point of interest is that I'm seeing a javascript error saying
    _sahi._checkbox("checkedIds[" + $arrPicks[$j] + "]") is null
    
    when I use the FOR loop, like the loop is running into the end of the array instead of paying attention to the loop condition.
  • Hi,

    Try this code:
    var $arrPicks = new Array();   
    var $len;
    _set($len, $arrPicks.length);
    for (var $j = 0; $j < $len; $j++) {
         _click(_checkbox("checkedIds[" + $arrPicks[$j] + "]")); 
    }
    
    ensure the array is populated.
  • narayannarayan Administrators
    Pankaj you are right about using _click that way. One further change:
    var $arrPicks = new Array();   
    /*
    Populate array here:
    Use 
    _set($arrPicks, getMyCustomBrowserIdsFunction()) 
    if you are populating the array from values on the browser page.
    
    Use 
    $arrPicks = ["a", "b", "c"] 
    if you are not dependent on any browser values to populate this.
    */
    var $len = $arrPicks.length;
    
    
    for (var $j = 0; $j < $len; $j++) {
         _click(_checkbox("checkedIds[" + $arrPicks[$j] + "]")); 
    }
    
  • StringyLowStringyLow Members
    edited September 2008
    It got my script to work by loading an array the Sahi way using a temporary array to carry the values:
    var $tempArray = new Array();
    
    <<< $tempArray gets loaded with random values here >>>
    
    var $realArray = new Array();
    _set($realArray, $tempArray.slice());
    
  • narayannarayan Administrators
    I will look at this (buggy) behavior. Thanks for highlighting it.
  • Still buggy in build 2011-05-25
  • Hi Abepetrillo,

    Are you facing any issue working with 2011-05-25?

    There are made bug fixed in the latest version of Sahi. Please try using it.

    Regards
    Sumitra
Sign In or Register to comment.