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 the 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.
Step 3: Click on the Downloads option under AutoIt.
Step 4: Click on the ‘Download AutoIt’ button to download AutoIt.
Step 5: Now download “Autoit editor” by clicking on ‘Editor Downloads’.
Step 6: As shown below click on the link.
After download you will get two setup files, The first is AutoIt version 3 setup and the second is SciTE4AutoIt3.
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'
Click on ‘SciTE.exe’ file, the AutoIT editor opens as shown in the below screen.
Step 8: Now opens the element Identifier.
Go to ‘C:\Program Files (x86)\AutoIt3’
And click on ‘Au3Info.exe’ file, the element identifier opens a screen as shown below.
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:
- 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’
- 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’
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 uploads 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.
In conclusion, integrating AutoIt with Selenium WebDriver provides a reliable solution for handling file upload scenarios and other non-browser interactions. By automating system-level tasks, this combination enhances the scope of your test automation framework. Gaining expertise in such integrations is essential for advanced testing, and Selenium online courses offer in-depth guidance on mastering these techniques. Enroll in a course today to enhance your skills and take your automation expertise to the next level.
Call to Action
Are you ready to tackle complex automation challenges like file uploads and non-browser interactions? Mastering AutoIt usage with Selenium WebDriver is a game-changer for creating robust and versatile automation frameworks. At H2K Infosys, we provide expert-led training that dives deep into integrating tools like AutoIt to handle system-level tasks seamlessly. With real-world projects and hands-on practice, you’ll gain the confidence to automate even the most complex scenarios effectively.
Join H2K Infosys and Advance Your Career
Don’t let system-level challenges hold you back in your automation journey. Enroll in our comprehensive Selenium online courses to learn how to integrate AutoIt with Selenium WebDriver for handling file uploads, pop-ups, and more. With experienced trainers, live projects, and 24/7 support, we ensure you’re equipped to excel in the competitive field of automation testing. Take the next step toward becoming a skilled automation professional join H2K Infosys today!