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.

Sahi and RichFaces FileUpload

wgoriswgoris Members
edited November -1 in Sahi - Open Source
Hello,

We have several web applications which are using the RichFaces FileUpload component (see http://livedemo.exadel.com/richfaces-demo/richfaces/fileUpload.jsf).

I cannot use the sahi _setFile for this component. Is there a way in sahi to upload a file with this component?

Thank you in advance.

Best regards,
Werner

Comments

  • wgoriswgoris Members
    Hi,

    In the meantime, I've already found a way to upload one file with the RichFaces FileUpload component.
    //assign the file that you want to upload to the file element of the component
    _setFile(_file("content:fileUploadForm:fileUpload:file"), $filePath + $fileName);
        
    //we added a hidden field 'sahiFileSelecterHelper'
    //we use this field to trigger the event that enables the upload button of the richfaces compontent on our testserver
    if (_isIE()){
        _call(_hidden("sahiFileSelectorHelper").outerHTML = _hidden("sahiFileSelectorHelper").outerHTML.replace(/type=hidden/, "type=text"));
    }
    else{
        _call(_hidden("sahiFileSelectorHelper").type = "text");
    }//end if
    // Set the filename into the textbox => file will be added in the richfaces component and upload button will appear
    _setValue(_textbox("sahiFileSelectorHelper"), $fileName);
    

    However, there remains one problem and I don't know how to solve this:
    There are validations performed on the file by the component (for example, it checks that the file has the correct extention). If this check fails, the component shows an error message and doesn't send the file to the server.

    But during the next test iteration, sahi still seems to remember the 'wrong' file and will upload this one to the server in stead of the new one.

    Is there a way to cleanup the files that were not send correctly with the _setFile function?
  • narayannarayan Administrators
    Hi Werner,

    That was a great work around you have figured out. Sorry we could not help out on that.

    To cleanup the file set via _setFile, use this:
    // Updated with working code
    <browser>
    function unsetFile (el, url) {
        if (!url) url = (_sahi.isBlankOrNull(el.form.action) || (typeof el.form.action != "string")) ? _sahi.getWindow(el).location.href : el.form.action;
        if (url && (q = url.indexOf("?")) != -1) url = url.substring(0, q);
        if (url.indexOf("http") != 0) {
            var loc = window.location;
            if (url.indexOf("/") == 0){
                url = loc.protocol+ "//" +  loc.hostname + (loc.port ? (':'+loc.port) : '') + url;
            }else{
                var winUrl = loc.href;
                url = winUrl.substring(0, winUrl.lastIndexOf ('/') + 1) + url;
            }
        }
        _sahi._removeMock(url.replace(/[.]/g, "[.]"));
    };
    </browser>
    _call(unsetFile(_file("content:fileUploadForm:fileUpload:file")));
    

    Explanation:

    _setFile() registers the form action URL with Sahi, so that the file is added to the request. This registration is removed once the file is successfully added. Since it never got invoked due to js validations, we need to explicitly unregister it. In the snippet above, much of the code is to get the form action URL. Once it is retrieved, the URL is de-registered using _removeMock

    Regards,
    Narayan
  • wgoriswgoris Members
    Hi Narayan,

    When I paste the code in my testscripts, I get the following error:
    _call(unsetFile(_file("content:fileUploadForm:fileUpload:file")));
    ReferenceError: unsetFile is not defined
    

    I also see a warning at the start of the test script in the log file:
    undefined: missing ( before formal parameters (http://tester-ubuntu.mmis.be:8282/prtr-beheer/swf/start/start-flow?execution=e2s3:12)
    
  • narayannarayan Administrators
    Sorry, copy paste errors in that reply. Please use the code below:
    <browser>
    function unsetFile (el, url) {
        if (!url) url = (_sahi.isBlankOrNull(el.form.action) || (typeof el.form.action != "string")) ? _sahi.getWindow(el).location.href : el.form.action;
        if (url && (q = url.indexOf("?")) != -1) url = url.substring(0, q);
        if (url.indexOf("http") != 0) {
            var loc = window.location;
            if (url.indexOf("/") == 0){
                url = loc.protocol+ "//" +  loc.hostname + (loc.port ? (':'+loc.port) : '') + url;
            }else{
                var winUrl = loc.href;
                url = winUrl.substring(0, winUrl.lastIndexOf ('/') + 1) + url;
            }
        }
        _sahi._removeMock(url.replace(/[.]/g, "[.]"));
    };
    </browser>
    _call(unsetFile(_file("content:fileUploadForm:fileUpload:file")));
    

    Regards,
    Narayan
  • wgoriswgoris Members
    Hi Narayan,

    Thanks for your help. However the piece of code didn't do the job. I'll describe the situation and what's going wrong:

    -Test1: Upload a correct file with extention .xml => OK, file is uploaded
    -Test2: Upload a wrong file with extention .txt => OK, file is not uploaded, because the button to upload the file is not visible and an error message appears
    -Test3: Upload again a correct file with extention .xml => NOK, the .txt file from test2 is still send to the server even when the function unsetFile is executed after the setFile in test2 or before the setFile in test3.

    However, I found a proper workaround (with the help of your first explanation):
    Since the event to show the button to upload the files or the error message is triggered by changing our own added hidden field, I wait to perform the _setFile method until I'm sure the file needs to be uploaded (meaning no error message is shown).
Sign In or Register to comment.