Create Script In AutoIt To Upload File

Before creating and running file upload script In AutoIT, You must be aware about what Is AutoIT and how to Install It. I have already described about AutoIT In THIS POST and how to download and Install It In THIS POST. Now let us learn how to create script In AutoIT to upload file using selenium webdriver.

In this section, We will learn how to create and run AutoIt script stand alone. Next post will describe you how to Integrate AutoIT script with selenium webdriver.

Identify objects properties of File Upload dialog
First of all you need to Identify objects of file upload dialog. Navigate to THIS PAGE where you will find file uploading functionality. Click on Browse button. It will open file uploading dialog as shown In bellow Image.

To Identify objects of dialog, AutoIt has Windows Info tool. You can open It from Start -> All Programs -> AutoIt V3 -> AutoIt Windows Info.


To Identify object properties of Upload dialog, Simply drag and drop "Finder Tool" from AutoIT v3 windows Info tool to "File name" field as shown In above Image. It will automatically Identify object properties of File Upload dialog and File name field. Same way you can Identify properties of any object of any dialog. We will use that object property In auto It script. In this example, Our object properties are as bellow.
  • File upload dialog : Title = File Upload, Class = #32770
  • File name field : Edit1 (Class + Instance combination)
  • Open button : Button1 (Class + Instance combination)
Write AutoIt script to select file In dialog
File "Test.txt" (which I wants to upload) Is located at E:\AutoIT location. To write file upload script, you need to use SciTE Script Editor. Open script editor from Start -> All Programs -> AutoIt V3 -> SciTE Script Editor and write bellow given script In to It and save It as "Script To Upload File.au3". Script explanation Is given with each line.

Note : Test.txt file should be located at "E:\AutoIT" folder.

; It will wait for 8 seconds to appear File Upload dialog.
; Used Title property of File upload dialog window.

  WinWait("File Upload","",8)

; Set control focus to File name Input box of File Upload dialog.
; Used Class property of File upload dialog window and Class+Instance property for File name Input box.

  ControlFocus("[CLASS:#32770]","","Edit1")

  Sleep(3000)

; Set the name of file In File name Edit1 field.
; "Test.txt" file Is located In AutoIT folder of E drive. So we have to provide full path like E:\AutoIT\Test.txt.

  ControlSetText("[CLASS:#32770]", "", "Edit1", "E:\AutoIT\Test.txt")

  Sleep(3000)

; Click on the Open button of File Upload dialog.

  ControlClick("[CLASS:#32770]", "","Button1");

In above script, We have used total five different AutoIT commands. Explanation Is as bellow.
  • WinWait : Used to wait for given time to appear targeted dialog window.
  • ControlFocus : Set the focus on targeted dialog window.
  • Sleep : Wait for given time.
  • ControlSetText : Write text In targeted Input text field of dialog.
  • ControlClick : Click on targeted button of win. dialog.
Your script will looks like bellow In AutoIt editor.



Convert script In executable format
To run this script, We need to convert It In executable .exe format. It Is very simple because AutoIT has built In function to convert script In .exe format.

To convert script, 
  • Simply go to the folder where "Script To Upload File.au3" file Is stored.
  • Right click on file and select Compile Script as shown In bellow Image. It will create "Script To Upload File.exe" file In same folder.


CLICK HERE to download ready made "Script To Upload File.au3" and "Script To Upload File.exe" files of AutoIt.

Test AutoIt script
Now you can test script to verify Is It working fine or not.

To test script, 
  • File Upload dialog should be opened. Go to THIS TEST PAGE In Firefox browser and click on "Browse" button to open File Upload dialog.
  • Double click on "Script To Upload File.exe" file to execute script and observe actions on File Upload dialog. It will enter file path In File name field of dialog and then click on Open button automatically.
If this works then your script Is fine. Read next post to learn how to use "Script To Upload File.exe" file In selenium WebDriver test to upload file.

4 comments:

  1. is it mandatory data "Test.txt" must be in E: driver only?

    ReplyDelete
  2. is it mandatory that "Test.txt" must be in E: drive only?

    ReplyDelete
  3. Hi, Aravind

    I am facing problems here.
    1. the information gotten by AutoIt Windows Info is different from that in this post. For example,
    In my AutoIt Window info there is "Edit", not "Edit1", is "Button" not Button1". So download the
    files not good now.
    2. I have to change the content of the file, but it is imposible for me to compile the .txt to .exe.
    Maybe this problem will be disappear, when using eclipse.

    so I'm not worry it.

    thanks.

    Mak

    ReplyDelete