Upload file using AutoIT

With the help of AutoIt tool (open source tool) we can upload by transferring the control from Selenium webdriver to AutoIt.

We need to explicitly call the AutoIt script from our program.

About AutoIT : AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting.

We need to call the AutoIt script after clicking on the upload button. Immediately after clicking on Upload button, the control should be transferred to AutoIt by the below statement which takes care of uploading the file.

If you have not installed AutoIt, please install AutoIt first and then proceed.

Syntax:

Runtime.getRuntime().exec("AutoIt .exe filepath");

The below is the sample HTML source code with upload button:
File Upload AutoIt

If you observe the above screen shot, there are is no 'Input' html tag. Button is completely customized using css. Here we cannot send the file path using sendKeys method. Hence we should go for other Tool support to handle Operating System events

The below is the example code to call the AutoIt exe file.

package com.easy.upload;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class UploadFileAutoIt {

	static WebDriver driver;
	String URL = "C:\\Users\\Harry\\Desktop\\samplehtml.html";
	@Test
	public void testUpload() throws InterruptedException, IOException
	{
		driver = new FirefoxDriver();
		driver.get(URL);
		WebElement element = driver.findElement(By.name("file"));
		element.click();
               //Which calls the autoit exe file
		Runtime.getRuntime().exec("G:/Tutorial/AutoItScripts/upload.exe");
	}	
}

The below is the AutoIt script:

WinWaitActive("File Upload")
Send("G:\Tutorial\AutoItScripts\TestScripts\Test.doc")
Send("{ENTER}")

Now what to do with above AutoIt Script. We will see how to work with the above script. The first and foremost thing we need to do is save the above script with '.au3' which is AutoIt file extension.

File Upload AutoIt

But if you observe we are passing '.exe' file to the script. And the file we have just saved is with '.au3' extension.

We actually need to generate the '.exe' file by compiling it into a standalone executable.

How to compile AutoIt file '.au3' to '.exe' file.

There are multiple ways to do that. But the simple way is using Right Click option. To do that, we will follow the below steps

Step-1: Navigate to the .au3 file that you wish to compile.
Step-2: Select the file and Right-click on it to access the pop-up menu.
Step-3: You will get an option as 'Compile Script'. Click on 'Compile Script.

File compile AutoIt

After performing above steps, it will generate a compiled file with the same file name with a .exe extension. Now pass this file to the test script. Thats It!!!!!

Hope this tutorial helps you. Please fell free to comment.

Selenium Tutorials: 

Comments

How to Upload different file at every execution , I am downloading one PDF file with different name at every test Execution and I want to upload that file on every execution (Recently downloaded file need upload).

Can You please help me, how to set dynamically downloaded file name (Path) in AutoIt Script.

WinWaitActive("File Upload")
Send("G:\Tutorial\AutoItScripts\TestScripts\Test.doc")
Send("{ENTER}")

This worked perfectly to me

I can able to upload the file using auto IT, thanks for your support

Hi,
do you know how to call an AutoIt script in Python?
Thx

Please let me know tool for mac for uploading file same as AutoIT

Hi,
I followed above steps and it is working perfectly on my local machine. Thanks for sharing detailed information.
But I am not able to performed on selenium grid node machine.
I have already installed AUTOIT on remote machine. upload.exe is executable file. Once I am opening browser manually and clicking on upload .exe manually, it is working perfectly. After running automated script I am getting error as Exception: Cannot run program "G:/Tutorial/AutoItScripts/upload.exe": error=2, No such file or directory
Could anyone please help me to resolve this issue?

I have already installed AUTOIT on remote machine. upload.exe is executable file. Once I am opening browser manually and clicking on upload .exe manually, it is working perfectly. After running automated script I am getting error as Exception: Cannot run program "G:/Tutorial/AutoItScripts/upload.exe": error=2, No such file or directory
could any one please help me here?

You cannot run this on a remote machine using selenium Grid as these files(G:/Tutorial/AutoItScripts/upload.exe and also the target upload file) are not present on the remote machine. These are only present in your local machine but you are executing your script on remote machine which cannot access files on your local machine. So you need to copy these files manually on the remote machine in the same path structure.

Unable to upload image using auto it

i am using chrome so i used "Open " command in .au3
should we run selenium script first and then auto it ?
what is this "file" ? in (WebElement element = driver.findElement(By.name("file"));)
should we keep the file that need to be uploaded inthe same folder where exe file is present ?

Nice article but is confusing regarding this questions

I am able to open the window after click on upload button with autoit but my code doesn't select the picture which I mentioned in my exe file . First opened the window then driver exited

Add new comment

Image CAPTCHA