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.

File upload Issue

ganeshprganeshpr Members
edited November -1 in Sahi - Open Source
I have already gone through few posts relating to this issue but could not find any solution.

I am trying to upload a file. The recorder generates:

_click(_div("...[1]"));
_setFile(_file(""), "test.XML");
_setFile(_file("file"), "test.XML");
_click(_div("Upload[1]"));

This does not work.

I tried to modify this into:

_click(_div("...[1]"));
_call(_file("file").type="text");
_setValue(_textbox("file"), "test.XML");
_click(_div("Upload[1]"));

I get a java script error on the application. This was tried in both IE and FF.

Snippet for the fileuplaod form. (Upload button is not part of the snippet)

<form target="FormPanel" action="../uploadFile" enctype="multipart/form-data" method="post">
<input type="file" name="file" size="70">
</form>

Any help would be greatly appreciated.

Comments

  • narayannarayan Administrators
    Hi,

    It should be a mix of both:
    _click(_div("...[1]"));
    _setFile(_file("file"), "test.XML");
    
    if (_isIE()){
        _call(_file("file").outerHTML = _file("file").outerHTML.replace(/type=file/, "type=text"));
    }else{
        _call(_file("file").type = "text");
    }
    
    _setValue(_textbox("file"), "test.XML");
    _click(_div("Upload[1]"));
    

    Let me know if this works, else we will have to dig in deeper.

    Regards,
    Narayan
  • 3vil3vil Members
    Hi Narayan,

    I tried to use the same scenario for Sahi Java API and couldn't find any method to do it... :-(

    Please, help
  • narayannarayan Administrators
    Hi 3vil,

    In the last released open source version, something like this should work
    if ("true".equals(browser.fetch("_sahi._isIE()"))) { 
        browser.execute("_sahi._file('file').outerHTML = _sahi._file('file').outerHTML.replace(/type=file/, 'type=text')");
    } else {
        browser.execute("_sahi._file('file').type = 'text'");
    }
    browser.textbox("file").setValue("test.XML");
    

    Regards,
    Narayan
  • Hi Narayan,

    I have tried this method in IE and Firefox. But in both it seems to fail.

    In IE - No result for the step execution. The next steps fail as the upload popup is not removed.

    In FF - I get a java script error if I change the type to text.

    Regards,
    Ganesh
  • Does anyone have any ideas to solve this?
  • If it helps, after the script stops at the file upload window, I tried to browse for a file manually. Then I see this output in the command prompt.
    url: {URL}/uploadFile
    command: FileUpload_appendFiles
    Feb 9, 2011 3:01:16 PM net.sf.sahi.command.FileUpload appendFiles
    INFO: Uploading: fileName = {FilePath} resolved to {FilePath}
    
  • My error is also the same and its critical for me. Could you please help?
  • Thanks to Narayan for help.

    For now, it is solved for Firefox. For Internet Explorer, Narayan and his team are looking into the solution. Will post once I receive from them.

    Solution for Firefox:

    The _setFile and changing of the type in combination did the trick.

    The java excerpt of the code is:
    
    // Instructing sahi to append file content as part of request
    browser.setFile(browser.file("file"), "test.XML");
    
    if ("true".equals(browser.fetch("_sahi._isIE()"))) { 
        // To escape UI validation on empty field
        browser.execute("_sahi._file('file').outerHTML = _sahi._file('file').outerHTML.replace(/type=file/, 'type=text')");
    } else {
        // To escape UI validation on empty field
        browser.execute("_sahi._file('file').type = 'text'");
    }
    // Continue to flow For Ex: Click on Upload button if you have one or Submit button
    
    
Sign In or Register to comment.