Upload a File Using AutoIT(WebDriver) - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Tuesday 24 September 2013

Upload a File Using AutoIT(WebDriver)

1. First install autoit from www.autoitscript.com/site/autoit/downloads
2. Create an autoit file with below code say abc.au3 file and create exe file for this file (abc.au3 file) using autoit say "attachFile.exe" and save it in "C:\\ABCAutomation"

Auto IT code:-

WinWaitActive("File Upload") 
Send("C:\Users\neeraj\Desktop\sample.doc") 
Sleep(1000)
ControlClick("File Upload", "", "Button1")

Save this code with extension.au3 and after saving Compile the Script to convert it into .exe file which AutoIt can run

Selenium code to Handle Upload button using AutoIT

 driver.findElement(By.xpath("//*[starts-with(@class,'a1')]")).click(); // To click browse button to open Dialog box
 String[] commands = new String[2];
 commands = new String[]{"C:\\Users\\neeraj\\Desktop\\autoitfile_upload.exe"}; //location of the autoit executable
 Runtime.getRuntime().exec(commands); // To run exe file and handle dialog box
 Thread.sleep(7000);


OR Directly write: Runtime.getRuntime().exec("C:\\Documents and Settings\\Neeraj.Bakhtani\\Desktop\\Browse.exe");

11 comments:

  1. used the above code but not able to proceed beyond open window to upload image

    please help

    ReplyDelete
  2. Check the name of Upload Pop up Menu, in my Case it was File Upload.
    Or share some details and screenshot to look into the issue.
    Thanks

    ReplyDelete
  3. Clicking on Upload button on the browser, it opens up a windows explorer window ("Open") and trying to select a file, but not getting any file output back, used as
    WinWaitActive("Open")
    Send("C:\Users\Public\Pictures\Sample Pictures\Desert.jpg")
    Sleep(1000)
    ControlClick("Open", "", "Open")

    ReplyDelete
  4. Hi Neeraj Ji,
    Thanks a lot for this wonderful site.
    Do you have any facebook/Google+ accounts.. where you can posts your content and status(when new content is published)...?
    Thank You..

    ~Chaitanya

    ReplyDelete
  5. Hi Chaitanya,
    Yes You can join the blog or you can follow by email, or Add google+.
    You will receive all latest posts updates

    ReplyDelete
  6. Hi Neeraj ! Can You Plz tell me how to import photo from Facebook in selenium webdriver.i am using java.

    ReplyDelete
  7. hi neeraj .. im trying to run autoit for uploading the file using ur mechanism.. what happens is i click the browse button named "open". then run the autoit exe but still nothing happens.. it just opens the dialog and nothing happens after that.. need some help on this

    ReplyDelete
  8. Hi Neeraj,
    I tried the same code for uploading file,it is working fine till capturing the path location after that it stops please let me know how can fix this issue.

    Thanks,
    Pavitra

    ReplyDelete
  9. Hi Neeraj,
    I used the same code which you have posted for file upload,it works fine till capturing path.After capturing the path its not selecting the given file,please let me know how to resolve this issue at the earliest.

    Thanks,
    Pavitra

    ReplyDelete
  10. how to make this code generic like I should be able to pass the path of the file that has to be uploaded from selenium java code to autoit code as a parameter.

    ReplyDelete