Uploading & Downloading files in Selenium using AutoIT

Reading Time: 3 minutes


It will help you to have a Setup the AutoIT tool in window machine and also help to Uploading & Downloading files in Selenium using AutoIT in window GUI.

Prerequisites

  • Java as the programming language
  • AutoIT as the automation tool
  • Maven as the build tool
  • WebDriver as the browser automation tool
  • IntelliJ as the IDE

What is AutoIt?

  • AutoIt is a scripting language implemented for windows GUI automation. Using the combination of simulated keyboard, mouse movement, and window/control manipulation to automate tasks not possible using only selenium. So use of AutoIt tool, We will Uploading & Downloading the files in Selenium.

Why Use AutoIt?

  • Selenium is an open source tool that is designed to automate web-based applications on different browsers but to handle window GUI and non HTML popups in application.  To handle such elements like Windows authentication box, File upload dialog box, and any other non-browser interaction we use AutoIt.

How to download and install the AutoIT

This image has an empty alt attribute; its file name is screenshot-from-2021-05-18-16-54-25.png
  • Install the AutoIT tool.
  • After installation completion – open up AutoIT Editor.
    Go to C:\Program Files (x86)\AutoIt3\SciTE\SciTE
This image has an empty alt attribute; its file name is screenshot-45.png
  • Click on the “SciTE” Application, The AutoIT editor opens as shown in the below screen.
This image has an empty alt attribute; its file name is screenshot-47-1.png
  • Now opens the element Identifier.
    Go to C:\Program Files (x86)\AutoIt3\Au3info_x64.
This image has an empty alt attribute; its file name is screenshot-49.png

Upload the file using AutoIt Script in Selenium WebDriver

  • Open the Programs – Autoit tool – SciTE Script Editor and include the below mentioned AutoIt script in Autoit editor and save it as ‘UploadFile.au3’ in your system.
  • Convert it as ‘UploadFile.exe’.
  • add the below mentioned Selenium Script and run.

Step 1: Open SciTE Script editor and add the below mentioned AutoIt script and save it as ‘UploadFile.au3’ in your system.

AutoItScript

ControlFocus("Open","","Edit1")
ControlSetText("Open","","Edit1","C:\Users\PrajjawalK\Music\check\visit.pdf")
ControlClick("Open","","Button1")

AutoItScript Explanation

  1. ControlFocus(” title “,” text “,controlID )
    Above the line of code changes the focus to the file upload windows.
  2. ControlSetText(” title “,” text “,controlID ,” File path which need to upload ” )
    set text/path into file name edit box.
  3. ControlClick(” title “,” text “,controlID )
    click open to upload file.

Step 2: Once the file is saved.

Step 3: Now we need to convert the ‘UploadFile.au3’ to ‘UploadFile.exe’. So Right-click on the file ‘UploadFile.au3’ and click on ‘Compile Script(x64)’ to generate an executable file ‘UploadFile.exe’.

Step 4: Add the below selenium script and run.

package Autoit;

import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class FileUpload {


public static void main(String[] args) throws InterruptedException, IOException {
//Instantiation of driver object. To launch chrome browser
System.setProperty("webdriver.chrome.driver","C:\\Users\\PrajjawalK\\Downloads\\Upload_Download\\Upload_Download\\chromedriver.exe");

WebDriver driver=new ChromeDriver();
driver.get("https://www.ilovepdf.com/pdf_to_word");
driver.manage().window().maximize();
driver.findElement(By.cssSelector("a[id='pickfiles']")).click();
Thread.sleep(3000);
//To call the AutoIt script
Runtime.getRuntime().exec("C:\\Users\\PrajjawalK\\Music\\check\\Fileupload.exe");
driver.close();
}

}

Download the file in Selenium

  • We use below script for downloading the file.
package Autoit;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import org.testng.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class FileDownload {

public static void main(String[] args) throws InterruptedException, IOException {

// for giving download directory
String downloadPath=System.getProperty("user.dir");
System.setProperty("webdriver.chrome.driver","C:\\Users\\PrajjawalK\\Downloads\\Upload_Download\\Upload_Download\\chromedriver.exe");

HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
// Set ChromePref and pass the download folder path with key
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadPath);
ChromeOptions options=new ChromeOptions();

options.setExperimentalOption("prefs", chromePrefs);

WebDriver driver=new ChromeDriver(options);
driver.get("https://www.ilovepdf.com/pdf_to_word");
driver.manage().window().maximize();

driver.findElement(By.cssSelector("a[id='pickfiles']")).click();
Thread.sleep(3000);
//autoit exe software for selecting file
Runtime.getRuntime().exec("C:\\Users\\PrajjawalK\\Music\\check\\Fileupload.exe");

Thread.sleep(2000);
driver.findElement(By.cssSelector("span[id='processTaskTextBtn']")).click();
WebDriverWait wait=new WebDriverWait(driver,30);
//its wait till page is totally loaded
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a[id='pickfiles']")));

driver.findElement(By.cssSelector("a[id='pickfiles']")).click();
Thread.sleep(5000);
File f=new File(downloadPath+"/visit.docx");
if(f.exists())
{
Assert.assertTrue(f.exists());
if(f.delete())
System.out.println("file deleted");
}
}
}




References

https://www.guru99.com/use-autoit-selenium.html


Knoldus-blog-footer-image

Written by 

Prajjawal is a QA Consultant. He is familiar with core concepts of manual & automation testing using tools like Selenium and Postman Also having knowledge of Python and Data Science. He is always eager to learn new and advanced concepts in order to improve himself. He likes to watch web series and play cricket.