How To Upload A File By Using Autoit Script

In Selenium WebDriver, to upload a file either we can use Robot class or we can use a third party tool like Autoit. In Robot class we need to write more number of steps but in Autoit script, you just need to write a small script, compile it, convert it in .exe and then call it in your WebDriver script. The very first step is to download Autoit Software in your system.

Downloading and Installing Autoit

  1. Go to google.com and write “download Autoit ” and click on the first link displayed.

d

2. It will navigate you to the autoitscript.com site. Now you need to go to the downloads options.

d1

3. Under downloads section, search for the Autoit installation option. Once you get it, click on it to download the file.

d2

4. Once you download this file, install it by following all the simple steps given at the time of installation. I think the installation procedure is not that tough so I am not going to explain the installation procedure here.

5. After the installation is done, you need to write an autoit script for your code of uploading file, and then you have to compile it and then include its .exe file to your rest of the code.

6. Go to some drive and right click on any blank space. Go to new and then select “Autoit v3 script” option.

7. Now write click on the created Autoit v3 script folder and chose the option “Edit script”.

d3

8. You need to edit the script now. Suppose you are writing for the http://www180.lunapic.com/ site, and in this site under Borders -> Border Tool option we get an option of Browse, where we need to chose a file from our local system.

9. Add the following code to the script :

$count = 0

While $count <> 10
$hdl =WinActivate(“File Upload”)

If $hdl <> 0 Then
ControlFocus(“File Upload”,””,”Edit1″)
Sleep(500)
ControlSetText(“File Upload”,””,”Edit1″,”E:\imgs\123.txt”)
Sleep(500)
ControlClick(“File Upload”,””,”Button2″)
Exit
EndIf
Sleep(1000)
$count = $count + 1
WEnd

d4

 

10. Explanation of the above script :

Open the site http://www180.lunapic.com/

Go to Borders -> Border Tool -> Browse

A window will be displayed showing you your local system files option. Now you have to switch to this window from your web page. To do this we are writing this autoit script.

Go to All Programs option of your system. There you will get something like this :

d5

Click on the AutoIt window Info Option.

Now open your script page and the above mentioned site.

d6

Now click on the Finder Tool Option and drag it to the File Name text box.

d7

When you drag the Finder tool on the File name text box, it gets you some information on the autoit info window. You will be able to see some details about the title, class, and interface.

d8

In the script, we have added a line ControlFocus(“File Upload”,””,”Edit1″) . Here we have written the text under title option in Info window page into the script in the ControlFocus function. In this function we have to provide three fields, first one is Title, next is text where we have left it as ” ” and the next one is the combination of Class and Instance i.e; Edit1.

Likewise we have wrote for the Open button.

d9

That is why we have written the line ControlClick(“File Upload”,””,”Button2″) in the autoit script.

Now you need to compile and build this script.

d10

If you do the compile and build, an .exe file will be created which you need to include in your java script.

d11

Here the .exe file is in D drive, so I will copy the path of this .exe file and will give it in the test script.

d12

The rest of the code is easy, you just need to inspect the web element and write the code. Now you have included your autoit script. You can run and check, the upload of file will happen.

Add Comment