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.

_setFile setting?

ghillie14ghillie14 Members
edited November -1 in Sahi - Open Source
Does anyone know if the _setFile command works? Trying to upload a document, but it is not setting the file.

_setFile(_file("ap_document_name"), "C:\\sahi\\TestPDF.pdf");

It just passes over this line without setting the file to be uploaded.

Would appreciate any comments about this, thanks!

Comments

  • narayannarayan Administrators
    _setFile works in a round about way. It is not handled at the browser level. It is handled at the proxy. So you will not see the file input box being populated with your desired filename. But when the form is submitted, the proxy will add the correct file to the request before it sends it to your web server. But if there are javascript checks before form submit to see if the filename is non-empty, then the script will not work as desired.
  • Also resolved this issue...I've changed the sahi source code quite a bit to fit our testing needs and feel comfortable with it but the _setfile wasn't working. So I was looking at the code on how the mime types were called. Then I looked at the $_POST dumps and this is what showed up...

    Without Sahi = [type] => application/pdf

    With Sahi = [type] => application/octet-stream

    So I simply added a new line in the /config/mime-types.mapping which was = .pdf application/pdf

    Now it works great. Hope this helps.

    Cheers Narayan
  • Hi,

    Is there any workaround to upload a file when there is a validation/check to check file name is not empty?

    Regards,
    Pankaj.
  • Any thoughts on this?
  • mmirskimmirski Members
    edited January 2009
    Actually I found one neat solution to that problem:

    First you go like with normal _setfile

    _setfile(_file("FileInputID"), "C:\Path\To\File.txt");

    Next you have to use own function which would change input file to input textbox :)

    Mine looks like this:

    <browser>

    function fileValue3()
    {
    var x=document.getElementsByTagName("input");
    x[4].outerHTML = '<input name="File1" type="textbox" id="File1" size="40" value="C:\\File.txt" />';
    return x[4].outerHTML;
    }

    </browser>

    Now you just invoke that function from the inside of sah script.

    You don't have to return outerHTML of that element - that's just for my debuging purpose.

    It will work if javascript checking did you actually fill you input will look for that element by its ID (because it didn't change).

    I believe using this (or changed a bit) way you can overcome input file problem.

    Narayan - I think you could implement such way of dealing with those input files automatically - so it would change input to textbox and fill it before submitting the form.

    Thanks,

    Maciej
  • Hi,

    This seems somewhat like html page hack that Narayan Sir was talking about in one of the related threads.
    Will try this one as soon as I get back to work :)
    Thanks for sharing.

    Waiting for Narayan's comments on this :)

    Thanks & Regards,
    Pankaj Kumar.
  • narayannarayan Administrators
    Maciej,
    Thanks for the nice hack. Note that this will work only on IE. On firefox, setting the value to a file field will throw a security exception. FF does not have an outerHTML property either. On FF, I had previously considered just changing the type of the element to text and then setting the value.
    _call(_file("fId").type = "text");
    _setValue(_textbox("fId"), "D:\\file.txt");
    
    But if some javascript verification code checks for file elements by checking "type=file", then this may fail. I have not yet come to a solution which will work transparently. Till then, let us document these hacks here so that people can use the work around that best works for them.

    One other intrusive way of handling this was to ask the developer to change the application code, such that he moves the file verification code into its own function. Then in your Sahi script, you just define an empty browser function with the same name. This will effectively prevent the file verification.

    Eg.
    Original app:
    function checkFileFields(){
      // Original code which checks file field and shows error message if invalid;
    }
    
    function verifyFields(){
       checkTextFields();
       checkFileFields();
    }
    
    Sahi code:
    <browser>
      function checkFileFields(){
        // Do nothing here. 
      }
    </browser>
    
  • Hi narayan,

    Can u please explain me tat where to use that input textbox which was explained by mmirski.

    <browser>

    function fileValue3()
    {
    var x=document.getElementsByTagName("input");
    x[4].outerHTML = '<input name="File1" type="textbox" id="File1" size="40" value="C:\\File.txt" />';
    return x[4].outerHTML;
    }

    </browser>
  • narayannarayan Administrators
    Priya,
    The file field is being converted into a text field. x[4] is the fifth element in his page which is a file field. So that element's outerHTML is reset to a that of a text field. Setting outerHTML on IE replaces the element with the new html.

    You could do
    _call(_file("fid").outerHTML = '<input name="File1" type="text" id="File1" size="40" value="C:\\File.txt" />');
    
  • Priya,

    I used javascript function mainly because I am not very familiar with Sahi (started last week :) ).

    If I knew I can do it by using _call internal sahi function I would do it this way or even the way Narayan described in the earlier post (by changing 'type' argument).

    Thanks,
    Maciej
  • Hi Narayan,

    Isn't there any possibilbity of sending a direct status(filled/empty) of the file field through the proxy which otherwise must be sent by the browser? just guessing :)

    Regards,
    Pankaj.
  • Hi Narayan,

    I have used _call like this :

    _call(_file("AssetFileUploaderForm:assetFileUploader:file").outerHTML = '<input name="assetDetailsForm:editAssetButton" type="text" id="assetDetailsForm:editAssetButton" size="20" value="C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Winter.jpeg" />');

    But i got the following error

    _call(_file("AssetFileUploaderForm:assetFileUploader:file").outerHTML = ''); TypeError: '_sahi._file(...)' is null or not an object
    No trace available
    Stopping script

    Here in the Veiw Logs a TextBox is created after ".outerHTML =" with this path(C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Winter.jpeg.) inside the TextBox

    And then i tried with this

    <browser>

    function fileValue3()
    {
    var x=document.getElementsByTagName("INPUT");
    x[180].outerHTML = '<input name="file" type="text" id="File1" size="20" value="C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Winter.jpeg" />';
    return x[180].outerHTML;
    }
    </browser>

    var $ret;
    _set($ret,fileValue3());
    _alert($ret);

    The name of the _file("AssetFileUploaderForm:assetFileUploader:file") was changed to a text box with that path inside which was previously named as "ADD...".
  • Hi Priya,

    So do you have problem with javascript validation of the input file element ?

    If not there's no need for you to change value of that particular element.

    Thanks,

    Maciej Mirski
  • Hi Maciej,

    Thanks for the reply
    I have not changed the value, i mean to say that when i use this

    x[180].outerHTML = '<input name="file" type="text" id="File1" size="20" value="C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Winter.jpeg" />';

    The ADD... button(Clicking on which opens up the file browse window) in my application as changed to a text box with this path filled in that and stops there,that is its not uploading the file.

    Thanks
    Priya
  • Priya,

    1) As a first step you have to use _file function to link file you want to upload to the <input type="file"> element.

    2) Next you can optionally change it to textbox to fill it with value (that's necessary only if there's any check if the input field for file is populated).

    Otherwise there's no need for you to do that.

    3) Last step would be submitting the form.

    ADD... button would disappear if the element it's linked to would disappear. In this particular situation it is effect of changing <input type="file"> to <input type="text">. (text doesn't have any browse or add button).

    Hope that helps,

    Maciej
  • sumeetsumeet Members
    It did not submit the form when I tried it changing type = text.

    Narayan, did you put any fix for this in recent builds?
  • ponsoponso Members
    I was able to upload my file using Ghillie14's method by adding to the mime; then I used "_setFile"
  • We had a similiar problem.
    The Upload button was calling a "file type validation" method and
    _click(_button("Upload"));
    didnt work.

    We worked around the problem
    by calling submit action instead of clicking "Upload " button
    _call(doOnSubmit());
    Lech
  • Hi there,

    I have the same problem of all of you regarding the usage of Javascript in order to check before form submit to see if the filename is non-empty.

    I read the documentation of Sahi, specifically the information under the URL which includes the description of the setFile property. I can see there is a solution, but my problem is that my script is written in Java and it must be kept this way, so I am not able to find the method "call" on it.

    Could anybody help me? Is there a solution for Java?

    Thanks a lot in advance.

    Josué
  • Hi Josué,

    Please use:
    if(browser.isIE())
                browser.execute("_sahi._call(_sahi._file(\"file\").outerHTML = _sahi._file(\"file\").outerHTML.replace(/type=file/, \"type=text\"))");
            else
                browser.execute("_sahi._call(_sahi._file(\"file\").type = \"text\")");
            browser.textbox("file").setValue("scripts/demo/uploadme.txt");
    

    Regards
    Sumitra
  • Hi Sumitra,

    First of all, thank you very much for your quick response. The code is working just great, but I've got another problem. The fact is that when I introduce the sentence to assign a value to the textbox:
    browser.textbox("file").setValue("scripts/demo/uploadme.txt");
    

    The text appears incomplete, like "scripts/de", using the string of your previous example.

    Maybe the next question may seem a little trivial but, how could I increase the size of the textbox right before setting the value?

    Regards,

    Josué
  • Hi Josué,

    The path appears incompletly but this is only for validation to check if there is some text present in the textbox "file" before submit.

    You can increase the size of the textbox in the HTML page by using : size="85"

    Here is an example:
    html>
    <head>
    <title>My Page</title>
    </head>
    <body>
    <form name="myform" action="http://www.mydomain.com/myformhandler.cgi" method="POST">
    <div align="center">
    <br><br>
    <input type="text" size="85" value="Sahi - Web Automation and Test Tool » Sahi - Web Automation and Testing Tool">
    <br><br>
    </div>
    </form>
    </body>
    </html>
    

    Regards
    Sumitra
  • It does not work yet, I have changed the property to text as follows:
    browser.execute("_sahi._call(_sahi._file(\"fileToUpload\").type = \"text\")");
    

    When I click submit the name is incomplete and it does not upload the file because the name in the textbox is not correct. I try to increase the size of the textbox with the following instruction:
    browser.execute("_sahi._call(_sahi._textbox(\"fileToUpload\").size = \"100\")");
    

    The size of the textbox is larger now, but not the text that appears inside it, which is still being the same.

    I have tried some alternatives but they did not seem to work. Any ideas?

    Kind regards,

    Josué
  • Hi Josué,

    What browser are you using?

    You will have to use the whole script below:
    if(browser.isIE())
                browser.execute("_sahi._call(_sahi._file(\"file\").outerHTML = _sahi._file(\"file\").outerHTML.replace(/type=file/, \"type=text\"))");
            else
                browser.execute("_sahi._call(_sahi._file(\"file\").type = \"text\")");
            browser.textbox("file").setValue("scripts/demo/uploadme.txt");
    

    It will check what browser you are using and executes accordingly.

    Also, post the contents of Sahi console.

    Regards
    Sumitra
  • I am using both Firefox and IE, but the result is the same.

    Regards,

    Josué
Sign In or Register to comment.