How to Use AutoIT In Selenium WebDriver

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.

download AutoIt Selenium WebDriver

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.

autoIt on windows start menu

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.

Handle Windows Security Dialog Box Using AutoIt in Selenium Webdriver

As per the above screenshot, we have to perform the below actions to enter the credentials and press the “OK” button.
  • 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.

AutoIT windows info Selenium WebDriver

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“.

get dialog box title using AutoIt in Selenium WebDriver

Now go to Start menu > All Program > AutoIt v3 > ‘SciTE Script Editor‘. It will open the AutoIt editor for writing the AutoIt script.

AutoIt aSciTE Script Editor

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)

In AutoIT, we do have a Send method which can be used to set a text value in a text field as well as send keyboard keystrokes. How does it work differently, depends upon how we pass the values in it.If you want to set a text value in a text field then the send command would as follows.
 

Send (“Required text Value”)

If you want to send a keystroke then the send command would as follows.

Send (“{KeyValue}”)

To know more about the Send function please refer to AutoIt help.
 
So as per the Tab Indexes mentioned in the Windows Security Dialogue Box screenshot, the complete script would be as follows.
 

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.

Run autoit script in selenium webdriver

The test log is displayed at the bottom of the script.

autoit-test-log-script-in-selenium-webdriver

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

Convert Autoit Script into exe file

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:

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:

How to Upload a File Using AutoIt in Selenium WebDriver?

In order to upload a file using AutoIt first of all, you will have to get the title of the file upload window. We will follow the same steps as we did in handling the Windows Security window using AutoIt. Open the file upload window and drag the finder tool in it.

UPLOAD A FILE USING AUTOIT IN SELENIUM WEBDRIVER

The title of the file Upload windows is “Open“.The default cursor location will be always at the File name field. Hence writing an AutoIt script will be quite easy to upload a file. It can be easily achieved in just three lines of code as shown below.
 

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:

Recommended Posts

Leave a Reply