Wednesday 27 November 2013

Window (File Upload/Download) in Selenium webdriver


File Upload/Download using Autoit
I would like to explain the Autoit tool on the file upload functionality. Download the tool from http://www.autoitscript.com/site/autoit/downloads/ , create the appropriate Autoit .au3 script, compile into exe file, test the exe file to verify whether the functionality is working as expected and integrate with selenium. Read the help file for better understanding.
Scenario:
I have web button on the page, when selected; browser opens the following window (attaching screen shot below). Selenium works fine till Browse button selection, now it is the responsibility of the Autoit script to upload the file so that selenium can continue with the remaining GUI activity.



Steps:
1. Select the File Upload window. Drag and drop the finder tool icon on the windows objects to get its corresponding properties following screen shot demonstrates how to open and capture the windows controls.











2. Open Script Editor (program files), write the code for ex.



AutoIt Script
WinWaitActive("File Upload")
Send("D:\demo.txt")
ControlClick("", "&Open", "Button1")



3. Save the file as demo.au3
4. Right click on this file and click ‘Compile Script’. This will create a demo.exe file at the same location.
5. Select open button, so that all the selection details are transferred into web page control and selenium can take control of the next activity.

Autoitdemo.java

public class Autoitdemo {
         
          public static void main(String[] args)  {
                   try{            
                  
                   WebDriver driver = new FirefoxDriver();
                   driver.get("http://www.sendspace.com/");
System.out.println(" Page Opened is: "+driver.getCurrentUrl()+"\n"+driver.getTitle() );
                   driver.findElement(By.xpath("//*[@id='upload_file']")).click();
                   System.out.println(" Browse button clicked");
                   Runtime.getRuntime().exec("D:\\demo1.exe"); //call Autoit script
                   System.out.println(" Execution Finished ");
                   driver.close();
                    }catch(Exception e)
                    {       
e.printStackTrace();
                   System.out.println(e.getMessage());    
                  
          }
          }
}

Java Code to call Autoit Script
Runtime.getRuntime().exec("D:\\demo1.exe");

For more help go to:




8 comments:

  1. Hi,
    Ruchi
    Why u use Third party tool for uploading file ?
    U can directly u can do it with KeyEvent
    u Check this If may I wrong then tell please
    public static void main(String [] args) throws InterruptedException, AWTException
    {
    WebDriver driver= new FirefoxDriver();
    driver.manage().window().maximize();
    driver.navigate().to("http://www.sendspace.com/");
    System.out.println(" Page Opened is: "+driver.getCurrentUrl()+"\n"+driver.getTitle());
    driver.findElement(By.xpath("//*[@id='upload_file']")).click();
    System.out.println(" Browse button clicked");
    Thread.sleep(2000L);
    filelocation="";//Please Store here location of the path
    StringSelection ss = new StringSelection(fileloaction);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);

    }

    ReplyDelete
  2. Hi Ruchi,

    thanks for the wonderful post, however i would like to inform u that this Autoit script is not working on chrome. Do you have a workaround for the same?

    ReplyDelete
  3. Both the solutions dont work on chrome any work around please let me know.

    ReplyDelete
  4. Thanq for u r post .... i have a doubt .suppose if i want to add multiple files at a time i could i do ?

    thanks in advance :)

    ReplyDelete
  5. Nice post even i have explained over here using auto it http://www.seleniumgyan.com/2014/11/how-to-upload-file-using-autoit.html
    and using Robot class http://www.seleniumgyan.com/2014/11/how-to-upload-file-in-selenium.html

    ReplyDelete
  6. Thanks for providing good content on "Download File Using Selenium WebDriver". http://reditblog.blogspot.in/

    ReplyDelete
  7. Thenq Ranjan your code help me

    ReplyDelete
  8. Thenq Ranjan your idea with robot help me

    ReplyDelete