Hello Ninjas. We know that Selenium is one of the best test automation frameworks. It is used for testing web applications. But there are also some limitations when using Selenium. It is not able to handle the operating system-level dialogs. It is also not able to automate the tasks that are outside the browser. This is where AutoIT comes into the picture.
In this article, we will discuss about AutoIT in Selenium. We will discuss about why it is used. This blog will also help us to understand the steps of using AutoIT in Selenium. If you are a beginner with Selenium, you must have a doubt about what Selenium is. Before moving on to the main topic, let us understand about Selenium.
What is Selenium?
Selenium is a widely used test automation framework. It is an open-source framework that makes it easy to use for everyone. It is primarily used for automating web browsers.
Selenium is one of the most preferable frameworks. It provides various tools and libraries. That’s why it enables you to automate web applications across different browsers. It is supported by multiple programming languages(Java, Python, C#, etc).
What is AutoIT?
Selenium is one of the best automation frameworks. But it comes up with some limitations:
It cannot automate tasks at the OS level.
It cannot automate tasks outside the web browser.
These above-mentioned limitations can be addressed by AutoIT. AutoIT is one of the scripting languages that is designed for Microsoft Windows. It is also a freeware automation tool. It can automate tasks at the OS level. It comes up with simple syntax and various functions. It can also automate the tasks that are present outside the web browser. AutoIT helps testers in many ways:
It allows you to interact with Graphical User Interface(GUI) elements.
It can simulate the keyword and mouse actions.
It can manipulate windows and dialogs.
It can handle various actions(file uploads, file downloads, interaction with pop-up windows, etc.).
Now you might have a doubt about how we can use it on our system. Let us look at the setup of AutoIT.
Setup of AutoIT
Before using it directly, we need to set up AutoIT and AutoIT script editor on our local machine.
Downloading and Installing AutoIT
First, we will follow below mentioned steps to download and install AutoIT:
Step 4: Now, click on the Download Editor button to download the editor.
Step 5: Now scroll down to the .exe file and click on it.
Step 6: Now, click on the save button.
Step 7: Now click on the downloaded file and install the editor. You have to click on the next button until you don't see the finish button.
Now, we have downloaded and installed the AutoIT and AutoIT script editor. Now, you might have a doubt about how to use it. Let us discuss this.
How to Use AutoIT?
To use AutoIT, we need to follow some steps that are mentioned below:
Step 1: Go to the location where you have installed AutoIT. So, for us, it is C:\Program Files (x86)\AutoIt3
Step 2:Now, select and open Au3info.exe. It is an elements finder.
Now we can drag and drop the finder tool to any website or web application. For example,
You can check this GIF, how the finder tool is getting information of a particular web page. We can use this finder tool to identify something on Windows by just simply dragging and dropping. Now you must be thinking of how to use it with Selenium. Let us consider an example of a website on which we need to upload something. So, we are taking the example of the file.io.
By clicking on the upload files button, we will see a different window:
This is the thing we cannot test using Selenium. That’s why we need to use AutoIT and test. We need to write some script to test this thing.
Step 3: Now, open the AutoIT script editor. Go to C:\Program Files (x86)\AutoIt3\SciTE and open SciTE.exe.
There are several commands that we can use in this editor, and a few of them are mentioned below:
ControlClick: It helps to simulate a mouse click on a particular window.
ControlFocus: It helps to set the input focus on a particular window.
ControlGetText: It helps to retrieve the text from a window.
ControlSetText: It helps to set the text of a window.
Step 4: Now, select any object that you want to work on. Suppose we are targeting the file name object.
Step 5: Now, drag and drop the finder tool in the file name.
Step 6: Now, we have to write the script for the file name object.
We have used several functions to perform the operation. These classes are given by the finder tool, as you can see in Step 5.
Step 7: Now, save the script with the .au3 extension.
Step 8:Now, we will compile the script.
After compiling this, it will generate the executable file. To check whether the script is working properly or not, we can run the script.
Now, we got an executable file, and we can use this executable file with Selenium to perform this task.
Use of AutoIT With Selenium
Now, we will understand how we can use the executable file in Selenium. There are some steps that we need to follow:
Step 1: Open Eclipse or any other IDE(Integrated Development Environment) and create a new Java project:
Step 2: Now, create a package CodingninjasAutoIT under src. Also, create a class SeleniumAutoITExample under the same package.
Step 3: Now, we need to download the Selenium WebDriver, Server, and ChromeDriver to work on our project. Go to Selenium’s official website. Firstly, we will download the server:
Now, we have to download the WebDriver:
Finally, we will download the ChromeDriver:
After downloading all the things, you need to extract the files one by one.
If you have already installed it, then you can skip this step.
Step 4: Now, we need to add these jar files to our project. So, firstly we will add the server. So we need right click on the project, then go to build path, and then go to configure build path:
Now, we have to click on Libraries and then add external jars:
Now, we have to select the server jar from our downloaded location:
Now, we have to apply this jar to our project:
Step 5: Now, in our class SeleniumAutoITExample, we need to set the property and path of our ChromeDriver.
Step 6: Now we need to write the following code in our class:
package CodingninjasAutoIT;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
// Creating a class SeleniumAutoITExample
public class SeleniumAutoITExample {
public static void main(String[] args) throws IOException, InterruptedException {
// Set the path of our chrome browser
System.setProperty("webdriver.chrome.driver", "C:\\Users\\naray\\Downloads\\chromedriver_win32\\chromedriver.exe");
// Using the WebDriver
WebDriver driver=new ChromeDriver();
// Using file.io to upload file
driver.get("https://www.file.io/");
// Class Selector
driver.findElement(By.className("e12cce780")).click();
// Running executable file to perform the operation
Runtime.getRuntime().exec("C:\\Users\\naray\\Downloads\\FileUploadScriptExample.exe");
Thread.sleep(4000);
driver.close();
}
}
This will give us the following output:
In this GIF, we can clearly see how Selenium is using the executable file to upload the file from the script.
Frequently Asked Questions
What is the use of AutoIt in Selenium?
AutoIT is a scripting language that is used in Selenium for automating OS-level tasks such as Windows-based pop-up windows, file uploads, and other non-browser interactions. It allows Selenium scripts to interact with desktop elements, enhancing test automation capabilities.
What is the purpose of AutoIt?
AutoIT is a scripting language and automation tool designed for Windows and its primary purpose is to automate tasks and interactions with Windows GUI elements, making it useful for automating desktop applications, and performing various system-related actions.
How to upload files using AutoIt in Selenium?
To upload files using AutoIT in Selenium, create an AutoIT script that opens the file upload dialog, navigates to the file, and simulates the "Open" button click. Then, call this script from your Selenium code using the Runtime.getRuntime().exec() method.
Conclusion
In this blog, we have discussed about AutoIT in Selenium. AutoIT is one of the important languages that is used in Selenium for scripting. We have discussed why it is used in Selenium, along with steps to use it. You can check out our other blogs to enhance your knowledge:
We hope this blog helped you to understand AutoIT in Selenium. You can refer to our guided paths on the Coding Ninjas Studio platform. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
To practice and improve yourself in the interview, you can also check out Top 100 SQL problems, Interview Experience, Coding interview questions, and the Ultimate guide path for interviews.