Integrating AutoIT Tool with Selenium
In this tutorial, we will learn Integration of AutoIT tool with Selenium WebDriver. While testing any web application, many a time we come across a situation where we have to interact with “Windows-based UI” elements. One of the most common scenarios is while uploading or downloading an image file.
Selenium WebDriver does not provide any inbuilt functionality to interact with Windows-based UI elements. In this situation, we have to take help of some third party tool. There are hundred’s of third party tools which are available in the market. AutoIT is one of the best open-source, easy to use tool available in the market.
Let us learn integration of AutoIT tool with a practical scenario.
Practical Scenario: Uploading an image on google:
Scenario:
- Open a browser of your choice (say Google Chrome)
- Navigate to “https://images.google.com/”
- Click camera icon –> Upload an Image –> Browse
- Window based UI opens up. Refer below images for same.
Now, to interact with this windows-based UI. We will use AutoIT tool.
Environment Setup for AutoIT:
Steps to follow:
- Download and install AutoIT tool and its editor.
Refer the screenshot below:
After installing AutoIT tool, open the editor and write the script. Now here, the script will be first, wait for the upload window to get active and then pass the absolute path of the image and then press enter.
Title of the window:
For Mozilla Firefox: Upload File
For Chrome: Open
// Wait till window with title appears WinActivate("Open") //Send the path of image file Send("C:\Users\Saurabh\Desktop\dekstop\Flower1.jpg") //Press Enter key Send("{ENTER}")
See the below screenshot for the reference.
After completing the script, to execute it first compile the code. After compiling the code an exe file will be generated.
Call this exe in your selenium code by below command:
Runtime.getRuntime().exec("C:\\workspace\\Seed16072016\\scripts\\Upload.exe");
Refer below code for complete scenario:
package day11; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.By; import org.openqa.selenium.chrome.ChromeDriver; public class FileUpload { public static void main(String[] args) throws IOException { System.setProperty("webdriver.chrome.driver", "C:\\Users\\sdhingra\\workspace\\libs\\chromedriver.exe"); WebDriver Driver = new ChromeDriver(); Driver.manage().window().maximize(); Driver.manage().deleteAllCookies(); Driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); Driver.get("https://images.google.com"); Driver.findElement(By.id("qbi")).click(); Driver.findElement(By.xpath("//span[contains(text(), 'Upload an image')]")).click(); Driver.findElement(By.id("qbfile")).click(); //To execute any exe file from java Runtime.getRuntime().exec("C:\\workspace\\Seed16072016\\scripts\\Upload.exe"); } }
This is how AutoIT tool can be integrated with Selenium.
For any questions, queries or comments feel free to write us at support@qatechhub.com or saurabh@qatechub.com. Happy Learning ?