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

can we use this for an ubuntu machine?

Runtime class is only available in Java. what's the other way to call .exe file in python?

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.