All IT Courses 50% Off
Selenium Tutorials

File Upload – AutoIT usage with Selenium Webdriver

What is AutoIt?

AutoIt is a freeware 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. An AutoIt automation script can be converted into compressed, stand-alone executable applications.

Why Use AutoIt?

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

In this tutorial, we will learn how to upload a file using AutoIt in selenium web driver. Here we need three tools 

  • Selenium Webdriver
  • AutoIT editor and element identifier
  • The window you want to automate

How to download and install AutoIT

Step 1: Go to the link https://www.autoitscript.com/site/autoit/downloads/

Step 2: Mouse-hover to AutoIt Install dropdown.

All IT Courses 50% Off
Autoit.PNG

Step 3: Click on the Downloads option under AutoIt.

autoitdownload.PNG

Step 4: Click on the ‘Download AutoIt’ button to download AutoIt.

autoitversion.PNG

Step 5: Now download “Autoit editor” by clicking on ‘Editor Downloads’.

editor.PNG

Step 6: As shown below click on the link.

zipeditor.PNG

After download you will get two setup files, The first is AutoIt version 3 setup and the second is SciTE4AutoIt3.

downloads.PNG

Step 6: Installing AutoIT-Click on both AutoIT setup.

Step 7: After installation completion – open up AutoIT Editor.

Go to 'C:\Program Files (x86)\AutoIt3\SciTE'

autoitexe.PNG

Click on ‘SciTE.exe’ file, the AutoIT editor opens as shown in the below screen.

exeopen.PNG

Step 8: Now opens the element Identifier.

Go to ‘C:\Program Files (x86)\AutoIt3’

autoitinfo.PNG

And click on ‘Au3Info.exe’ file, the element identifier opens a screen as shown below.

frozenautoit.PNG

Note: Once this element identifier is done you need to close it manually as it will not close automatically.

1. File Upload using SendKeys method in Selenium WebDriver:

We can easily achieve this by using sendkeys method, text box locating, and by using the sendkeys method we need to set the path of the file and click on the submit button.

package htmldriver;

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

public class Upload {

public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
//Instantiation of driver object. To launch Firefox browser
System.setProperty("webdriver.gecko.driver","F:\\drivers\\geckodriver.exe");  
     WebDriver driver = new FirefoxDriver();
//To open URL "http://softwaretestingmaterial.com"
driver.get("https://www.timesjobs.com/candidate/register.html?");
//Locating 'browse' button
WebElement browse =driver.findElement(By.id("resumeFile_basic"));
// By using Sendkeys method pass the path of the file to be uploaded Thread.sleep(3000);
browse.sendKeys("D:\\Testing\\UploadFile.txt");
//This close method will close the browser window
driver.close();
}

}

2. Upload the file AutoIt Script in Selenium WebDriver:

  1. 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
  2. Convert it as ‘UploadFile.exe’
  3. In Eclipse, 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.

AutoIt Script:

WinWaitActive("File Upload")
Send("D:\Testing\UploadFile.txt")
Send("{ENTER}")

AutoIt Script Explanation:

Line 1: WinWaitActive(“File Upload”)

Above the line of code changes the focus of cursor on the Window popup box to upload the file.

When using Mozilla Firefox ‘File Upload‘ is the name of the popup window. 

Line 2: Send(“Path of the document”)

Once the popup window is active, it will set the path of the document which needs to be uploaded

Send(“D:\Testing\UploadFile.txt”)

Line 3: Send(“{ENTER}”)

It clicks on Open button that will upload the document

Step 2: Once the file is saved, we need to convert the ‘UploadFile.au3’ to ‘UploadFile.exe’. To prepare this we need to compile the ‘UploadFile.au3’

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

compilescript.PNG

Step 4: In Eclipse, add the below Selenium Script and run

package htmldriver;

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;

public class Upload1 {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//Instantiation of driver object. To launch Firefox browser
System.setProperty("webdriver.gecko.driver","F:\\drivers\\geckodriver.exe");  
     WebDriver driver = new FirefoxDriver();
//To open URL "http://softwaretestingmaterial.com"
driver.get("https://www.timesjobs.com/candidate/register.html?");
//Locating 'browse' button
WebElement browse =driver.findElement(By.id("resumeFile_basic"));
//To click on the 'browse' button
browse.click();
//To call the AutoIt script
Runtime.getRuntime().exec("D:\\Testing\\AutoIt\\Uploadfile.exe");
//'close' method is used to close the browser window
driver.close();
}

}

In the above Selenium Script, After clicking on the browser button we call the AutoIt Script which transfers the windows popup box and upload the required file.

Syntax:

Runtime.getRuntime().exec(“File Path of AutoIt.exe”);

Runtime.getRuntime().exec(“D:\\Testing\\AutoIt\\Uploadfile.exe”);

Conclusion:

  • Download and install the Element Identifier and AutoIT editor.
  • Open the site to do the operation.
  • Identify the elements of the file uploader window using the Element Identifier.
  • With the help of Element identifier, we will prepare AutoIT script in the editor 
  • Autoit script is written in the selenium webdriver script.
  • Execute the selenium script.
Facebook Comments

Related Articles

Back to top button