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.

How to handle ajax file upload

jeesonmathewjeesonmathew Members
edited February 2012 in Sahi - Open Source
How to handle ajax file upload

I tried with below 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("dealImage")));
_setFile(_file("dealImage"), "image5.png");
if (_isIE()){
    _call(_file("dealImage").outerHTML = _file("dealImage").outerHTML.replace(/type=file/, "type=text"));
}else{
    _call(_file("dealImage").type = "text");
}
_setValue(_textbox("dealImage"), "image5.png");

but it returns error :
_call(unsetFile(_file("dealImage")));
TypeError: el.form is null

OS : Windows XP
Browser : Mozilla 3.6.6
Sahi Build: 2011-07-19

Thanks in advance
Jeeson

Comments

  • Hi Jeeson,

    File upload should work the same way as it would without AJAX.
    Have you looked at _setFile?

    Do you get the same error when you run the script without _call(unsetFile(_file("dealImage")));? If yes, is it possible for you to point us to your application or a similar example available online? We could try reproducing the problem and give you a solution.

    Regards,
    dkulkarni
  • Hi Dkulkarni,
    Thanks for your reply.

    Yes, same error on without _call(unsetFile(_file("dealImage")));

    Image uploader in our application is similar to http://www.webinfopedia.com/demo/ajax-dynamic-image-upload/

    Thanks
    Jeeson
  • let me know that whether it is possible or not?

    Thanks
    Jeeson
  • jeesonmathewjeesonmathew Members
    edited February 2012
    No Reply !!!! Please anyone reply.
  • At last I tried with mouse movement using java.
    photo_upload();
    
    function photo_upload () {
        var robot = new java.awt.Robot();
        robot.keyPress(java.awt.event.KeyEvent.VK_END);    
        
        var mouseX=java.awt.Toolkit.getDefaultToolkit().getScreenSize().getWidth() ;
        var mouseY=java.awt.Toolkit.getDefaultToolkit().getScreenSize().getHeight(); 
        
        var mouseX=450;
        var mouseY=700;    
        robot.mouseMove(mouseX, mouseY);     
        robot.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
        robot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
        robot.delay(2000);
        
        mouseX=50;
        mouseY=250;    
        robot.mouseMove(mouseX, mouseY);    
        robot.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
        robot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
        robot.delay(2000);
        
        mouseX=150;
        mouseY=120;    
        robot.mouseMove(mouseX, mouseY);    
        robot.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
        robot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
        robot.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
        robot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
        robot.delay(2000);
        
        mouseX=250;
        mouseY=120;    
        robot.mouseMove(mouseX, mouseY);    
        robot.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
        robot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
        robot.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
        robot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
        robot.delay(2000);
        
        mouseX=150;
        mouseY=120;    
        robot.mouseMove(mouseX, mouseY);    
        robot.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
        robot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
        robot.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
        robot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
        robot.delay(2000);
        
    }
    

    Thank You for the support!!
Sign In or Register to comment.