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.

for loop, $i and _setFile

tower120tower120 Members
edited November -1 in Sahi - Open Source
Hi!

When I use $i variable in for loop it increases each time
function uploadFile($elementID, $fileName){
    _setFile(_file($elementID), $settings.getUploadDir()+$fileName, "upload/file_from_swf?action=upload");
}


for (var $i=1; $i<=13; $i++) {
    _alert($i);

    $fname = "op_hd\\"+$i+".png";

    $oname = "picture_file_"+$i;

    _alert($i);
    uploadFile( $oname, $fname );
    _alert($i);
    _alert("ende");
}



After _setFile command $i increases. Without it all seems OK.
Also, if $i replace with $ij - it also ok.

Comments

  • Hi tower120,

    Could you please be more specific about the issue.

    Regards
    sumitra
  • Well, normally it should alert in this order:
    1
    2
    3
    4
    5
    6
    etc.

    with _setFile inside loop it act like this:
    1
    3
    5
    7
    9
    etc.

    Put some file input in the form and try to do _setFile() inside for $i loop.
  • Hi tower120,

    I had verified running below Sahi script on below mentioned html page :
    function uploadFile($elementID, $fileName){
        _setFile(_file($elementID), $fileName);
    }
    
    for (var $i=1; $i<=13; $i++) {
        _alert($i);
        $fname = "c:/qhdebug.log";
        //$fname should have a valid filepath in your system
        $oname = "fileupload0";
        uploadFile( $oname, $fname );
    }
    

    Note: There are chances that $i might be used as global variable and some where else $i is getting incremented.

    <html>
    <form name="f1" action="test1.html">
    <input type="file" id="fileupload0" name="fileupload1">
    </form>
    </html>

    Here is the output that i am getting,

    1
    2
    3
    4
    5
    6
    etc.

    Regards
    Sumitra
  • >> There are chances that $i might be used as global variable and some where else $i is getting incremented.

    But
    for (var $i=1; ...
    
    does not that mean that I use local variable $i inside loop?

    By the way, why I cant use variables without $ ?
  • Hi tower120,

    When you write
    _alert(i);
    Sahi will search for the variable i on the browser (and will not get it)
    When you write
    _alert($i);
    Sahi will evaluate $i on the proxy and pass it to _alert on the browser.

    Also, Can you post the full Sahi script?

    Regards
    Sumitra
  • Please, provide email. I cant post it here, it consists several .sah files, and contains passwords for httpAuth.
  • Hi tower120,

    You can email to sumitra@sahi.co.in

    Regards
    sumitra
  • I sent you email.
  • CarbinCarbin Members
    Good article and contains lots of valuable information. That is the experience that you shared with us. Thank you so much.
Sign In or Register to comment.