File Upload using AutoIT in Selenium





File Upload using AutoIT in Selenium will discuss about how we can upload a file into web application while automating the application using selenium webdriver. As we know that selenium webdriver automates only browsers (i.e. web applications only). But sometimes we might need to automate windows based applications/popups such as upload a file or download a file etc…

But selenium can not handle these kind of scenarios. So, we will use some third party tools to automate these type of functionality. Especially uploading a file is one of the scenarios we will face while automating most of the applications. For this we will depend mostly some third party tools like AutoIt and Sikuli.

In this blog we will look into AutoIt to automate file upload functionality in selenium webdriver. “AutoIt is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying “runtimes” required!”.

To work with AutoIt, need to download AutoIt(i.e.AutoIt program files, documentation and examples.) and AutoIT Script Editor(i.e. Customised version of SciTE with lots of additional coding tools for AutoIt). Once you download these things you will get an IDE to write the code and a Finder Tool to inspect the elements. Then you can write the code and save. Extension of the saved file is “.au3”, it will not be directly executed by our selenium code. For this we need to create an executable file for this au3 file. Right click on the au3 file then click “Compile Script” then it will generate the exe file. Then it will used in the selenium code to achieve the file upload functionality.

Steps to Download and Implement the AutoIT in Selenium:

1. Go to https://www.autoitscript.com/

2. Hover on “AutoIT” menu tab and click on Downloads option. Then you can find the below screen. Click on “Download AutoIt” button.

AutoIt Download




3. Hover on “AutoIT Editor” menu tab and click on Downloads option. Then you can find the below screen. Click on “ SciTE4AutoIt3.exe” link.

AutoIt Editor Download

4. Then you can find the downloaded 2 files in the path where you mentioned. Can find the same in the below screenshot.

Downloaded Software

5. Double click and install both the softwares.

6. Then you can find a folder called “AutoIt3” under the Program Files of the C drive. i.e. C:\Program Files (x86)\AutoIt3

7. Once you go into this folder then can find another folder called “SciTE”. Go this folder then can see a file called “SciTE”. Can find the same in the below screenshot.

AutoIt Script Editor




8. It will open script editor to write autoit scripts. Can find the editor in the below screenshot.

AutoIt Editor

9. Come out of “SciTE” folder to “AutoIt3” folder then we can see a file called “Au3Info_x64”. Can find the same below

AutoIt Finder Tool

10. It will open Finder Tool to identify the elements. Can find the editor in the below screenshot.

Finder Window




11. Once all these are ready, now we will open the application and find the elements to upload the file. For this will go to http://demo.automationtesting.in/Register.html link.

12. Click on Browse button then it will open the File Upload window popup.

13. Now we need to open the AutoIt script editor and Finder tool to inspect the elements and write the necessary script.

14. The steps here are:

  • Identify the Window Name
  • Identify the File Name input box and enter the file path which you want to upload
  • Identify the Open Button and click.

15. Below is the sample code to upload the file.

ControlFocus(windowTitle,text,controlID);
ControlSetText(windowTitle, text,controlID,fielPath);
ControlClick(windowTitle, text, controlID);

16. In the above three methods, text parameter is optional and we can leave blank. But we need to provide other parameters.

17. Once you open all the windows, need to hold and drag the finder tool icon to the element which we want to inspect or identify. Can find the same in the below screenshot.

Finder Tool Demo

18. Once you drag the finder tool, it will give the properties of the element. In the finder tool itself. Can find the same in the below screenshot.

Showing Properties on Finder Tool

In the above screen, we can find the Window Title and Control ID(Classname NN) from the Control tab.




19. This way you can capture the script for the file upload. Can find the same in the below screenshot.

Sample Program in AutoIt Editor

20. Save the above program as “FileUpload”. Then it will create a file with the extension “.au3”

21. Right click on the above file and compile, then it will create a FileUpload.exe file.

22. Then we can call and run this exe file from our selenium code using below code

Runtime.getRuntime().exec("exe file path");

23. Then our code will open the browser and click on Browse button and it will upload the file into the application.

Now we will see sample program to achieve the same:

Below is the AutoIT script:

ControlFocus("File Upload","","Edit1")
ControlSetText("File Upload","","Edit1","C:\Users\Krishna\Desktop\Clean.bat")
ControlClick("File Upload","","Button1")

Below is the sample selenium code which will call the above script and upload the file in the application:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FileUpload
{
    public static void main(String[] args) throws Exception
    {
        System.setProperty("webdriver.gecko.driver", "F:\\Selenium\\Drivers\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        // This Site is useful to practice basic Selenium
        driver.get("http://demo.automationtesting.in/Register.html");
        driver.findElement(By.id("btnAttachment")).click();
        Thread.sleep(5000);
        Runtime.getRuntime().exec("F:\\AutoIt\\FileUploadForFirefox.exe");
    }
}

This way we can upload a file in selenium using AutoIt.

Please watch the youtube video for better understanding.