There are a few limitations of Selenium WebDriver and one of them is that it cannot perform any action on windows GUI. If you are looking to automate a web application using Selenium WebDriver wherein you need to handle the Windows Security Dialogue box or download & upload files, you might get stuck. These are a few examples where AutoIt can be very useful to overcome such scenarios that are beyond scope of Selenium Webdriver.In this article, I will show you how to use AutoIt in Selenium WebDriver.
What is AutoIT?
AutoIt is a Windows Automation and Scripting Language. It is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement, and window/control manipulation to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt is also very small, self-contained, and will run on all versions of Windows out-of-the-box with no annoying “runtimes” required!
AutoIT Download and its Installation
Please visit https://www.autoitscript.com/site/autoit/downloads/ page to download AutoIt. Scroll down until the Current Versions section is displayed. Under this section, the latest version of AutoIt would be available with a download button.
Click on the download button and save it to your local machine and run the .exe file to install it. The installation process is quite simple, just press the “Next” button until the “Finish” button is displayed and complete the installation. After completing the installation it would be available under windows Program Menue.
Handle Windows Security Dialog Box Using AutoIt in Selenium Webdriver
Let’s just understand how Windows Security Dialogue can be automated using AutoIt by just simulating keystrokes in Selenium.
- Set the UserName as the default cursor position will be on the “User Name” field
- Press Tab to switch to Password field
- Set the Password value
- Press Tab (Now the focus would be on the checkbox)
- Again Press Tab (Now the focus would be on the “OK” button
- Send Enter Key to submit the credentials.
How to write an AutoIt Script to handle the Dialogbox in Selenium WebDriver?
Open the “AutoIt Window Info” utility from the Programs menu. It works in the same fashion as Object Spy in UFT Or Element Inspector in any Browser. To perform an operation on any open window first we need to have its title.
To get the window title, drag the ‘Finder Tool‘ box to the Windows Security Dialog Box. It is showing the window title as “Windows Security“.
Now go to Start menu > All Program > AutoIt v3 > ‘SciTE Script Editor‘. It will open the AutoIt editor for writing the AutoIt script.
Now we can write the AutoIt script in the editor. Remember one thing that the first statement of the script would always be a wait statement to wait for the required widow to get it appeared.
In the editor write the following code to wait for the Windows Security Dialogbox to get it appeared. This wait command would wait for infinite to wait for the desired window to get appeared.
WinWaitActive(“Windows Security”)
If you want to wait for only a certain time you can use the following wait statement. It will wait for ten seconds to wait for the required widow to get appeared.
WinWaitActive(“Windows Security”,””,10)
Send (“Required text Value”)
If you want to send a keystroke then the send command would as follows.
Send (“{KeyValue}”)
1 2 3 4 5 6 7 |
WinWaitActive("Windows Security") Send ("TESTUSER") Send ("{TAB}") Send ("Pass12345") Send ("{TAB}") Send ("{TAB}") Send ("{ENTER}") |
Now run the script to check whether it works. Go to “Tools > Test Run”.Before running make sure the required window is active. If not already in active mode, do it manually and ensure the control is on the UserName field after running the script.
The test log is displayed at the bottom of the script.
When the script runs successfully, please save it in .au3 format. The next step is to convert it into the .exe format. For that, you need to right-click on the .au3 file and select “Compile Script“.A .exe file will be created in the current folder with the same name as the AutoIt Script. I have saved the .exe file as Login.exe
The AutoIt .exe file can be invoked using Java by the following code:
Runtime.getRuntime().exec(“E:\\AutoItscripts\\Login.exe”);
The complete Selenium code for Login into Security Dialigbox in IE Browser will be as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.io.IOException; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetExplorerDriver; public class TestWindowsAuthentication { public static void main(String[] args) throws IOException, InterruptedException { System.setProperty("webdriver.ie.driver","C:\\BrowserDrivers\\IEDriverServer.exe"); WebDriver driver = new InternetExplorerDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //Provide application URL driver.get("Provide the URL"); //Invoking Autoit script to do login on Windows Security Dialogbox Runtime.getRuntime().exec("C:\\AutoItscripts\\Login.exe"); Thread.sleep(5000); //Write your Automation code after login //********* driver.close(); } } |
If somehow it doesn’t work then before navigating to the URL, place the following piece of code that invokes the AutoIt script .exe file like below:
1 2 3 4 5 |
//Invoking AutoIt Script before opening the URL Runtime.getRuntime().exec("E:\\AutoItscripts\\Login.exe"); //Opening the URL driver.get("Provide the URL"); |
How to Upload a File Using AutoIt in Selenium WebDriver?
WinWaitActive(“Open”)
Send (“E:\TestFiles\Log.txt”)
Send (“{ENTER}”)
Save the AutoIt script and compile it to create the executable file. Call this executable file in your script before clicking the file Open Window. The AutoIt script will start executing and as the file open window open it would immediately set the file path and click on the “Open” button.
The complete script would be as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.io.IOException; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetExplorerDriver; public class TestFileUpload { public static void main(String[] args) throws IOException, InterruptedException { System.setProperty("webdriver.ie.driver","C:\\BrowserDrivers\\IEDriverServer.exe"); WebDriver driver = new InternetExplorerDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("TEST URL"); //Provide the URL of the Test application //Invoking the AutoIt Script to start executing upload the file Runtime.getRuntime().exec("E:\\AutoItscripts\\LoadFile.exe"); //Clicking on the button to open File Upload Window driver.findElement(By.id("OpenFile")).click(); //Click Browse button Thread.sleep(3000); //As soon as the file Window will Open the AutoIt Script will find the file upload window and upload the file //Write your further automation code //******* driver.close(); } } |
Recommended Posts
- Handle Dynamic Web Tables In Selenium WebDriver
- Select Value From Multi-Select Dropdown In Selenium WebDriver And Dropdown with CheckBoxes
- Top #23 Most Useful Selenium WebDriver Commands That You Must know
- XPath in Selenium WebDriver with Example
- How to Use CSS Selector in Selenium WebDriver | 13 Examples
- Actions Class in Selenium WebDriver
- Double Click and Right Click in Selenium with Examples
- Wait Commands in Selenium WebDriver | Implicit | Explicit | Fluent Wait