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 doesn't work for me

hari19hari19 Members
edited November -1 in Sahi - Open Source
Hi Experts,

I have a form which uploads an XML file.
The scenario is to select an xml file and then press the Import button.
I'm using Sahi V2 Release 2009-05-21 and Firefox 2.0.0.20 browser on Windows XP SP3.

I tried :

a)
_setFile(_file("importFile"), "C:\\test.xml");
_click(_button("Import"));

b)
_call(_file("importFile").type = "text");
_setValue(_textbox("importFile"), "C:\\test.xml");
_click(_button("Import"));

c)
_call(_file("importFile").type = "text");
_setValue(_textbox("importFile"), "C:\\test.xml");
_call(_textbox("importFile").type = "file");
_click(_button("Import"));

d) Tried Modifying the xml using javascript
<browser>
function setImpFile()
{
    var x = document.getElementsByTagName('input');
    for (var i=0;i<x.length;i++) {
    if (x[i].type != 'file') continue;
    var newAtt = document.createAttribute("value");
    newAtt.nodeValue = "C:\\test.xml";
    x[i].setAttributeNode(newAtt);
    }
}

</browser>

_call(setImpFile());
_click(_button("Import"));

e) Setting the mime type for xml [ as suggested in sahi.co.in/forums/viewtopic.php?id=48 ]

f) Modifying the document and setting the value of the field.
var x = document.getElementsByTagName('input');
for (var i=0;i<x.length;i++) {
if (x[i].type != 'file') continue;
x[i].value="C:\\test.xml";
}

I get a "NS_ERROR_DOM_SECURITY_ERR: Security error" error


None of the above worked.

How can I get this working ?
Is there a way to enable logging in the Sahi Proxy that could help debug the issue ?
Any pointer is welcome and appreciated.

thanks,
Hari
Tagged:

Comments

  • narayannarayan Administrators
    What error (on the Sahi console) do you get when you use
    _setFile(_file("importFile"), "C:\\test.xml");
    _click(_button("Import"));
    

    ? Do you get any javascript errors or validation errors?

    Does your application use a single URL for all requests or is the form submitted to a unique URL (different from URLs of other pages) ?
  • hari19hari19 Members
    edited July 2009
    There are no errors /exceptions in the Sahi console.
    I also did not get any javascript / validation errors.

    The console has the following text added after running this test case :
    url: h ttp://myHost:8080/myApp/configure/fileimport.jsp
    command: FileUpload_appendFiles
    Uploading: fileName = C:\test.xml
    

    The form is submitted to the same URL. It does not change per request etc.

    My app complains with an error message / stack trace about "Content-Length: 0" which looks like the file does not seem to be uploaded.
    The same works when I do the upload manually.

    NOTE :
    There is no space between h and ttp in the sahi console.
    The forum does not allow me to post URLs since I'm a new member.
  • narayannarayan Administrators
    Could you try C:/test.xml once. Also try C:\\doesnotexist.xml and check what the output is. Meanwhile I am looking at the code here.
  • hari19hari19 Members
    edited July 2009
    With C:/test.xml => No difference compared to using C:\\test.xml
    In this case my app throws an exception ( same as before )

    With C:/testAXX.xml ( a non-existent file ), I see the following error in the Sahi console :
    url: h ttp://myHost:8080/myApp/configure/fileimport.jsp
    command: FileUpload_appendFiles
    Uploading: fileName = C:/testAXX.xml
    java.lang.reflect.InvocationTargetException
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            at java.lang.reflect.Method.invoke(Method.java:585)
            at net.sf.sahi.command.CommandExecuter.execute(CommandExecuter.java:52)
            at net.sf.sahi.command.MockResponder.getResponse(MockResponder.java:64)
            at net.sf.sahi.ProxyProcessor.handleDifferently(ProxyProcessor.java:205)
            at net.sf.sahi.ProxyProcessor.processAsProxy(ProxyProcessor.java:123)
            at net.sf.sahi.ProxyProcessor.run(ProxyProcessor.java:86)
            at java.lang.Thread.run(Thread.java:595)
    Caused by: net.sf.sahi.util.FileNotFoundRuntimeException: java.io.FileNotFoundException: C:\orgAXX.xml (The system cannot find the file specified)
            at net.sf.sahi.util.Utils.readFile(Utils.java:139)
            at net.sf.sahi.util.Utils.readFile(Utils.java:126)
            at net.sf.sahi.command.FileUpload.appendFiles(FileUpload.java:60)
            ... 10 more
    Caused by: java.io.FileNotFoundException: C:\orgAXX.xml (The system cannot find the file specified)
            at java.io.FileInputStream.open(Native Method)
            at java.io.FileInputStream.<init>(FileInputStream.java:106)
            at net.sf.sahi.util.Utils.readFile(Utils.java:136)
            ... 12 more
    

    And the browser shows an empty page ( Looks like the request did not go thru to my app )

    Thank you for your help!
  • narayannarayan Administrators
    Could you check the mime type of file received by the server? Was it expecting something else?
  • hari19hari19 Members
    I used LiveHTTPHeaders to see the request as sent by Firefox.

    A snippet for the request when using Sahi :

    1428251156286
    Content-Disposition: form-data; name="importFile"; filename=""
    Content-Type: application/octet-stream

    and there is no file content here.

    When doing the same file upload manually I see :
    29756655028618
    Content-Disposition: form-data; name="importFile"; filename="test.xml"
    Content-Type: text/xml

    <content of test.xml here>


    So it looks like the mime type seems to be a problem.
    I had added the following line to sahi's mime-types.mapping
    .xml text/xml
    and restarted the sahi proxy server.

    I havent actually checked the mime type recieved by the server.
    I'm using Tomcat 5.5. I'll have to figure out how to get Tomcat to check this since I did not write the application.
    Any quick ideas.

    I'm thinking of using the SOAP TCP Tunnel/Munitor in the following way.
    Sahi / Firefox ==> Proxy === > Tomcat
  • hari19hari19 Members
    I tried using the SOAP TCP Tunnel and here are the results :

    With SAHI :
    
    -----------------------------789343420475
    Content-Disposition: form-data; name="id"
    Content-Length: 0
    
    
    -----------------------------789343420475
    Content-Disposition: form-data; name="command"
    Content-Length: 6
    
    Import
    -----------------------------789343420475
    Content-Disposition: form-data; name="activeControl"
    Content-Length: 0
    
    
    -----------------------------789343420475
    Content-Disposition: form-data; name="importFile"; filename="test.xml"
    Content-Type: text/xml
    Content-Length: 444
    
    <myxml>
    ...
    ...
    ...
    
    </myxml>
    -----------------------------789343420475
    Content-Disposition: form-data; name="showMessages"
    Content-Length: 4
    
    true
    

    Without SAHI :
    -----------------------------6412110693438
    Content-Disposition: form-data; name="id"
    
    
    -----------------------------6412110693438
    Content-Disposition: form-data; name="command"
    
    Import
    -----------------------------6412110693438
    Content-Disposition: form-data; name="activeControl"
    
    
    -----------------------------6412110693438
    Content-Disposition: form-data; name="importFile"; filename="test.xml"
    Content-Type: text/xml
    
    <myxml>
    ...
    ...
    ...
    
    </myxml>
    -----------------------------6412110693438
    Content-Disposition: form-data; name="showMessages"
    
    true
    

    As you can see SAHI seems to be adding a Content-Length header to all the form data, which is not present when directly using a browser.

    My App also complains about a Content-Length:0

    Is it possible to suppress the Content-Length header ?
    If not by default if some configuration option can be provided to suppress the Content-Header it would be great!

    Does my analysis make sense ?
  • narayannarayan Administrators
    Make sense? It is awesome :)

    What you say could very well be the reason. I just tested it with some other test pages and it seems to be working there. So it is specific to your application. How urgent is the need? I can send you a build with that content length configurable and we can check if it works. It will take some time though.

    Regards,
    Narayan
  • hari19hari19 Members
    I would like a fix pretty soon since a lot of my app's configuration is in the form on xmls that I import.
    I certainly don't need it before Monday.

    I will also look at the code and try figuring out a crude hack that works for me.

    Thank you for your support!
  • narayannarayan Administrators
    Emailed you a build with content-length removable via sahi.properties.
  • hari19hari19 Members
    Thank you!
    I received the build.

    I will test as soon as I can and let you know how it went..

    Thanks Again!
  • hari19hari19 Members
    The fix works partly.
    The fix doesn't remove the Content-Length header for all the multipart entries.
    Here is the log from the Sahi proxy window when I attempted the same test as before with the new sahi.jar and sahi.properties updated :

    Please see that the Content-Length for the test.xml :
    Content-Disposition: form-data; name="importFile"; filename="test.xml"
    Content-Type: text/xml
    Content-Length: 444 <===== PROBLEM!
    

    Here's the complete output from the Sahi proxy :
    >>>> Browse any page and CTRL-ALT-DblClick on the page to bring up the Sahi Controller
    In post requestFromBrowser.data() = id=&command=login&activeControl=&accountId=configurator&password=configurator
    url: http://myHost:8080/myApp/configure/fileimport.jsp
    command: FileUpload_appendFiles
    Removing Content-Length
    Content-Disposition: form-data; name="id"
    Removing Content-Length
    Content-Disposition: form-data; name="command"
    
    
    Removing Content-Length
    Content-Disposition: form-data; name="activeControl"
    Removing Content-Length
    Content-Disposition: form-data; name="importFile"; filename=""
    Content-Type: application/octet-stream
    Removing Content-Length
    Content-Disposition: form-data; name="showMessages"
    
    
    Uploading: fileName = C:/test.xml
    In post requestFromBrowser.data() = -----------------------------4169407012788
    Content-Disposition: form-data; name="id"
    
    
    -----------------------------4169407012788
    Content-Disposition: form-data; name="command"
    
    Import
    -----------------------------4169407012788
    Content-Disposition: form-data; name="activeControl"
    
    
    -----------------------------4169407012788
    Content-Disposition: form-data; name="importFile"; filename="test.xml"
    Content-Type: text/xml
    Content-Length: 444
    
    <myxml>
    ...
    ...
    ...
    
    </myxml>
    -----------------4169407012788
    Content-Disposition: form-data; name="showMessages"
    
    true
    -----------------------------4169407012788--
    
    
  • hari19hari19 Members
    edited July 2009
    Narayan provided another build which fixes the issue that I reported.
    Now it works perfectly!

    Thanks Narayan for the quick response to my all updates on this issue!
  • narayannarayan Administrators
    Keats,

    I did not notice this before, but you are doing a _setValue(_file("ODTPath"), "smoke.odt");

    It should be _setFile(_file("ODTPath"), "smoke.odt");

    (It may still fail if you have javascript validations to check if a file has been set. Ping me if that is the case)

    Regards,
    Narayan
Sign In or Register to comment.