How To Handle Windows Pop Up In Selenium Using AutoIT

This Tutorial Explains Various Methods to Handle Windows Pop Up In Selenium Whenever Automation Goes out of Scope Using the AutoIT Tool:

AutoIT is a third-party tool that gives Selenium a helping hand to handle pop-ups like Windows Authentication pop up, Flash applications, Desktop application and so on.

Selenium is limited to automate web applications only. In some cases, a Windows pop up might appear in the application which Selenium can’t handle. In order to handle such scenarios, AutoIT gives a helping hand wherein it takes care of automating Windows pop up and desktop applications.

=> Visit Here To See The Selenium Training Series For All.

How To Handle Windows Pop Up In Selenium Using AutoIT (1)

Overview Of AutoIT Tool

Here is a Video Tutorial:

Handle Windows Pop Up Using AutoIT

AutoIT is a third-party tool that is used to handle Windows-based applications. The scripting language used is called VBScript. AutoIt is also a freeware. It uses a combination of mouse movement and keyboard strokes to automate the actions that are not possible to be done by selenium web driver.

AutoIt is used in .exe format so that it can be used in the Selenium Automation script.

Let's look at an example, wherein we would need to upload a file to the application.

The scenario is to upload a profile picture on LinkedIn. The Test steps for this scenario are:

  1. Enter URL
  2. Enter a valid username, password, and log in.
  3. Click on the profile upload icon.
  4. Windows pop up appears wherein the path to upload a picture is entered.

Until Step 3, Selenium Automation can handle it. When Windows pop up appears, the AutoIT script is used to automate that part.

Now, let us have a look at how to automate actions in the Windows pop up?

This can be handled in 4 different ways.

  1. Using Windows Commands to upload a particular file.
  2. Using Control Commands to upload a file.
  3. Using the command-line argument to specify the FileName to be uploaded.
  4. Using the AutoIT record tool.

All the scripts are created using AutoItScriptEditor, which is available in the AutoIt folder package.

Navigate to AutoIt3>SciTE>SciTE.exe and open the ScriptEditor application.

Open the ScriptEditor

#1) Using Windows Commands

We have various sets of functions in AutoIt.

You can have a look here. Now in order to automate the Windows pop up actions, lets first analyze the actions. These include:

  1. Set the focus to the current window.
  2. Enter the path of the filename to upload in FileName Box.
  3. Click on the Open button or Press Enter in Keyboard.

Using Windows Command

Let's understand the above points in detail:

  1. Set the focus to the current window

WinWaitActive: Sets the focus of the cursor to the Window name specified. In this case, the title of the window is ‘Open’. So we specify the command as WinWaitActive(“Open”).

Please note that the same Windows title differs for each browser.

Say, for Firefox, it is ‘File Upload’,  — So AutoIT command will be WinWaitActive(“File Upload”).
For IE, it is ‘File To Upload’  — So AutoIT command will be WinWaitActive(“File To Upload”)
For Chrome, it is ‘Open’ — So AutoIT command will be WinWaitActive(“Open”)

You should specify the window title based on the browser that you use for automation.

2.Enter the path of the filename to upload in FileName Box

Send – sends keyboard strokes or in other words, it simulates keyboard actions.

Actions might be pressing on the Enter Key, Shift Key, so on. OR typing a string, number,etc.. In this example, I need to type in the fileName, so I use the command to enter the Filename in the window. – Send(“D:\passport.jpg”)

3. Click on Open button or Press Enter in Keyboard

Send({ENTER}) – sends ENTER keystroke to the window, which in turn completes the action of upload.

Save the script created in any location. It will get saved in .au3 format. Once saved, right-click the .au3 file and compile it.

Save the script created

After compilation, it gets converted to .exe format (executable format). This file is what we use in the Selenium script.

.exe format

Once the .exe file is created, you can use the file in the Selenium Automation script to run the commands in it

Runtime.getRuntime.exec(“specify autoIt exe filename here”)

Runtime command

#2) Using Control Commands

We have certain control commands in AutoIT to perform actions. Here is the list of control commands in AutoIT.

list of control commands in AutoIT

Let us see how to automate the same file upload scenario using these control commands of AutoIT. 

Now, in order to automate the Windows pop up, the below actions are to be performed:

  1. Set the focus to the current window.
  2. Enter the path of the filename to upload in FileName Box.
  3. Click on the Open button or Press Enter in Keyboard.

Let's understand the above points in details:

  1. Set the focus to the current window

ControlFocus: Sets focus to the element specified. In this case, we set focus to the edit text box.

Control focus

When you look at its description, you can see that it requires one or more parameters. We can pass these parameters by using the Windows Info tool of AutoIt.

Let's open AutoIT application as below:

open AutoIT application

The Windows Info tool gets opened as below:

Windows Info tool

This tool is used to inspect elements in the Windows application/pop up.

In order to get various attributes of each element in the Windows pop up, drag the ‘Finder’ icon of the Windows Info Tool and click on the element to display its properties as below.

Windows Info Tool properties

We have its properties displayed in the tool as shown.

For the FileName box, the properties and its corresponding values are :

  • Title – Open
  • ClassName – Edit
  • Instance – 1

Using these properties, we can pass the parameters for the control command.

Now, in order to set focus to the FileName box, we call the command ‘ControlFocus’ as below:

  • Syntax : ControlFocus ( “title”, “text”, controlID )
  • The ‘title' corresponds to the title displayed in the tool.
  • Control Id is the combination of ClassName + Instance in tool

ControlFocus(“Open”,””,”Edit1”)

Where,

  • Open – is the title
  • Edit1 – is the controlID (controlID = ClassName+Instance)

2. Enter the path of the filename to upload in FileName Box

ControlSetText – types in the text to the element.

  • Syntax : ControlSetText ( “title”, “text”, controlID, “new text” [, flag = 0] )

ControlSetText

In our case, we have the filename in the text box.

ControlSetText(“Open”, ”” ,”Edit1”, “D:\passport.jpg”)

Where,

  • Open – is the title
  • Edit1 – is the controlID (controlID = ClassName+Instance)
  • D:\passport.jpg – is the filename to upload

3. Click on Open button or Press Enter in Keyboard

ControlClick – performs a click action on the element. In this example, we perform click on ‘Open’ button

  • Syntax : ControlClick ( “title”, “text”, controlID [, button = “left” [, clicks = 1 [, x [, y]]]] )

ControlClick

In our case, it should click on ‘Open’ button on Windows pop up.

ControlClick(“Open”,””,”Button1”)

Where,

  • Open – is the title
  • Button1 – is the controlID (controlID = ClassName+Instance)

We call the .exe file from Selenium code using the command-  Runtime.getRuntime.exec(“specify autoIt exe filename here”)

call the .exe file from Selenium code

NOTE: When we have to upload multiple files at a single instance, we can specify all the filenames in a variable and pass the variable name in the command.

specify all the filenames in a variable

ControlFocus – sets focus to the element.
$files – is the variable created and it holds the text/filename to be uploaded.
ControlSetText – passes the variable that has the file names to be uploaded to the element ‘Edit’.
ContolClick – performs a click action on the element.

#3) Using Command-Line to Specify the Filename to Upload 

The same procedure can be approached in a different way wherein the Filename is obtained from the user. This is done using the Command-Line approach.

Command-Line approach

Instead of directly specifying the filename in AutoIT, we pass it in the Selenium code as below:

AutoIT Script : (Upload.exe)

ControlFocus(“Open”,””,”Edit1”)
ControlSetText(“Open”,””,”Edit1”,$cmdLine[1])
ControlClick(“Open”,””,”Button1”)

In selenium Code:

We specify it as :

Runtime.getRuntime.exec(“Path of .exe file”+””+ “FileToUpload”)

In this case, it will be

Runtime.getRuntime.exec(“D:\automation\Upload.exe”+””+ “D:\automation\image.jpg”)

Save the file created and compile it to convert it to executable format.

#4) Using Record Feature of AutoIT

This feature is available only in versions below 3.3.14.0. Versions greater than that do not support this feature.

To open the Record tool, Navigate to AutoIt3>Extras>Au3Record>Au3Record.exe

Record Feature of AutoIT

When we start using this, always make sure to deselect the ‘Record Mouse’ option since the mouse interaction varies for each screen resolution. Always record the keyboard strokes.

Select ‘Click to Record’ in the tool and the actions get recorded in it.Select ‘Click to Record’

When you are done recording, select ‘Click to Stop’ and save the file

You can open the .au3 file saved to see the commands created for each action that you performed

.au3 file

Compile the script and convert it to .exe file.

The .exe file created can be directly used in the Selenium code for execution as below:

Runtime.getRuntime.exec(“D:\automation\Record.exe”)

Conclusion

Thus, any Windows pop up like authentication or file upload or open file etc. can be handled using AutoIT by including its script file in Selenium.

 In the next chapter, we will learn about another third-party tool called Sikuli which is used to automate applications using the Image recognition technique.

=> Visit Here To Learn Selenium From Scratch.