Our Engineers can Write as well

How to upload multiple files through Selenium using AutoIT?

Selenium does not support file uploads to a HTML5-based web application from local directory system. However, this limitation of Selenium can be overcome using a third-party tool called AutoIT in 2 steps:

  • Create AutoIT code that uploads file to an application
  • Call this AutoIT code inside Selenium script
AutoIT Installation Process:
Step 1:

Go to the link – https://www.autoitscript.com/site/autoit-script-editor/downloads/, select the latest version of AutoIT exe file and download the same.

Step 2:

Once you have downloaded the file, you may be prompted to read/accept the laid-out terms and conditions. To consent and proceed further, click “I agree” button as shown below.

AutoIT
Step 3:

Navigate to your Program Files, select Autoit3 folder and browse through AutoIT folder structure as shown below.

AutoIT

Now, let understand how to proceed with AutoIT.

AutoIT User Guide
Finding Properties – Step 1:

Go to AutoIT folder and find out the au3info control and click to view the properties. Note the Finder Tool.

AutoIT
Finding Properties – Step 2:

To find the properties of a user interface control, drag the Finder Tool and drop on the control.

Refer the following screenshot. Drag and drop the Finder Tool on the ‘File name’ drop down control in the ‘File Open’ pop up window. AutoIT v3 window displays the properties of the window (Basic Window Info) and the control (Basic Control Info).

AutoIT
How to upload multiple files through Selenium – Video Tutorial

To make it easy for you to understand, we have prepared a video tutorial for uploading multiple files through Selenium using AutoIT.

Uploading multiple files through Selenium using AutoIT
Scripting – Step 1:

Go back to the main AutoIT folder and search for “SciTE” folder. Once inside the SciTE folder, click on the control. A plain screen appears as shown below.

AutoIT

With the help of above SciTE exe editor screen, write the required automation code for uploading the file in AutoIT.

Scripting – Step 2:

There are basically three commands to upload a file and they are:

  • ControlFocus(“title”, “ text”, “ControlID”)
  • ControlSetText(“title”, “text ”, “ControlID”, “Path”)
  • ControlClick(“title”, “text”, “ControlID” )
ControlFocus: This gives focus to the window under context.

The ControlFocus has three parameters: Title is window title, text is optional and ControlID is combination of class and instance of the control (File name drop down).Refer Fig 4, AutoIT Finder Tool identifies window title as ‘Open’ and ‘File name’ Control ID as ‘Edit1’.

ControlFocus(“Open”, “”, “Edit1”)

ControlSetText: This sets the file path.

The ControlSetText has four parameters: Title is window title, text is optional, ControlID is combination of class and instance of the control and Path is location of the file (chosen for upload).

ControlSetText(“Open”,””, “Edit1”, “‪‪‪‪C:\Users\…\Downloads\Resume.doc ‬‬‬‬‬‬‬‬‬‬‬‬”)‬‬‬‬‬‬‬‬ //single file‬‬‬‬

ControlSetText(“Open”,””, “Edit1”, $CmdLine[1]) //multiple files

ControlClick: This will click the upload button.

In the below screenshot, window title is found through the AutoIT Find tool as ‘Open’ and window ControlID is combination of class and instance of the control (Open button).

ControlClick(“Open”, “”, “Button1” ).‬‬‬‬

AutoIT

Write the code in SciTE exe editor screen and save the file in .au3 format and then compile the saved au3file. This file is saved as FileUploader.exe under Scripts folder in E drive.

Sample Selenium code:

The below code explains how to upload a file using Choose File button through Selenium test automation script.

package com.auto;
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Demo {
public static void main(String[] args) throws InterruptedException, IOException {
System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\..\\Downloads\\chromedriver.exe”);
WebDriver driver =new ChromeDriver();
driver.get(“https://focusinfotech.com/Reg/”);
Thread.sleep(3000);
File file = new File(“E:\\Test”);
File[] files = file.listFiles();
for(File f: files){
String filename =f.getName();
driver.findElement(By.xpath(“//*[@id=’ use_var_TNresume’]”)).click();
Thread.sleep(3000);
Runtime.getRuntime().exec(“E:\\Scripts\\FileUploader.exe”+” “+”E:\\Test\\” +filename);
Thread.sleep(3000);
}
driver.close();
}
}

polam suresh reddy - software test engineer

Contributed by:

Suresh PolamReddy
Software Testing Engineer

Your email address will not be published. Required fields are marked *