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

Hi,

I can able to upload single image using AutoIt. but when i'm trying to upload multiple images showing error "incorrect expression"..

nice to hear from you.

U can upload multiple images by using for loop

Could u plz provide the how to write for loop for Multiple images

Hi,

I am curious what kind of tools can be use for mac? Because AutoIT is not available in mac, only for Windows.
Thanks.

Hi,
You can use Sikuli ,it is os independent.

How to upload multi files in single shot in open dialog box from desktop? (file names are too long)

Hi,
Can we drag and drop a file from windows file dialogue box?

Hi, Thanks for your valuable information . I tried that code but my generated exe of autoit doesn't work means it compile but not run. I am using win 7 32 bit .
waiting for reply

AutoIT is not working in IE Browser

Actually am automating my outlook mail application , am facing problem at click of browse button in a pop up window after clicking attachment button I have tried with many ways to click on browse button but didn't get. if anyone have tried plz could give reply as soon as possible.

Add new comment

Image CAPTCHA