EDITING BOARD
RO
EN
×
▼ BROWSE ISSUES ▼
Issue 52

Automate it with AutoIt

Zita Szőcs
Software Engineering Analyst @ Accenture
TESTING

Do you want an application that automatically starts up your favorite or most indispensable applications? Write an AutoIt script! It will ease your routines and launch everything you need for work while you enjoy your morning coffee.

While there are many great automation tools like Selenium, Sikuli, AutoHotkey available, AutoIt is easy to learn, it is freeware and it is fast - perfect for task automation based on GUI and for carrying out all kinds of tests related to performance!

AutoIt is considered one of the fastest automation tools for Windows applications. Its BASIC-like scripting language is designed for automating the Windows Graphical User Interface (GUI) and general scripting. It can simulate keystrokes, mouse movements and click events, and manipulate Windows forms and controls to automate tasks.

Initially designed in 1999 to help system administrators automate and configure thousands of PCs, AutoIt has evolved into a programming language that supports complex expressions, user functions, loops and everything else other scripting languages can. Today, this tool is widely used for more than just test automation. Key applications include automating routine actions related to operating systems, such as systems management, monitoring, maintenance, or even software installation.

Get writing

To write an AutoIt script, knowledge of fundamental coding principles is a must. However, the manufacturer does offer a range of tutorials.

An AutoIt automation script can be converted into a stand-alone executable with the built-in script compiler Aut2Exe. The executable can be run on computers that do not have the AutoIt interpreter installed. AutoIt is also distributed with an IDE based on the free SciTE editor which allows you to write, build, and run the scripts.

When you install the AutoIt package, it usually comes with two interesting and useful utilities:

The Au3Record can be used to record actions which can be turned into an AutoIt script. The code generated by the Au3Record isn't always reliable, however. It is based on mouse movement and clicking, so if you would like to use your script with another screen resolution, you would have to adapt it using other functions, like Treeview, TabControl, ControlClick, etc.

The Au3Info is a standalone tool which helps you get information such as window titles, size and position, which can be used to effectively automate an application.

Starting your frequently used apps

Let's say that we would like to start:

Running an application using AutoIt script can be done with several functions. These include ProcessWait, ProcessWaitClose, Run, RunAs, RunAsWait, ShellExecute, and ShellExecuteWait. The most commonly used is the Run() function, which simply starts the requested program (with the full path given as parameter) and lets the script continue.

After running the application, it is useful to pause the execution of the script until the requested window exists. This can be carried out with one of the following functions: ProcessWait, WinActive, WinExists, WinWaitActive, WinWaitClose, or WinWaitNotActive.

The script can also be paused with the Sleep() function, by calling it with the amount of time to pause expressed in milliseconds.

So we can start our Web browser with the following code:

; Run Google Chrome
Run("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")
; Wait for window to appear
WinWaitActive("[Intermediate D3D Window]","",10)

Once it starts, we can type the webpage address, press Enter, open a new tab, and repeat the previous steps:

; Open a new tab with Ctrl+t
Send("^t")
Sleep(1000)
; Type the address and press Enter in one command
Send("https://www.autoitscript.com/autoit3/docs/{ENTER}")
; Open a new tab with Ctrl+t
Send("^t")
Sleep(1000)
; Type the address and press Enter in one command
Send("https://news.ro{ENTER}")

To open Windows Explorer and go, for example, to “C:\Program Files (x86)\AutoIt3” work from Run dialog directly:

; Simulate the key combination Win + R to open the Run dialog
Send("#r")
; Type "C:\Program Files (x86)\AutoIt3" and press Enter
Send("C:\Program Files (x86)\AutoIt3")
Send("{ENTER}")

Or we can use the toolbar section from Windows Explorer:

; Open My Computer (File Explorer in Win10) and wait until becomes active
Send("#e")
$hWnd = WinWaitActive("File Explorer")
; Click on the Address bar (in this case is called toolbar)
ControlClick($hWnd,"","[CLASS:ToolbarWindow32; INSTANCE:3]","left")
Sleep(500)
; Send the address and press Enter
Send("C:\Program Files (x86)\AutoIt3")
Sleep(1000)
Send("{ENTER}")

Opening a file automatically from the File menu is ideally done using the WinMenuSelectItem() function, but this works only on standard menus. The SciTe editor window has no standard menu, so adapt the solution by using key commands and Windows keyboard shortcuts.

; Open the SciTe editor
$hWnd = RunWait("C:\Program Files (x86)\AutoIt3\SciTE\SciTE.exe")
; Click on File menu by pressing Alt+F
Send("!f")
; Press Down and Enter keys in order to select Open... option
Send("{DOWN}")
Sleep(500)
Send("{ENTER}")
$hWnd = WinWait("Open File")
; Put the focus on the Tree view and select Local Disk (C:) folder
ControlFocus($hWnd, "", "Tree View")
Send("Local")
; Expand the C drive, Search for AutoItScripts and expand it
Send("{RIGHT}")
Send("AutoItScripts")
Send("{ENTER}")
; Activate the folder, select and open the first file
Send("{TAB}")
Sleep(500)
Send("{DOWN}")
Sleep(500)
Send("{ENTER}")

Starting a source control application is similar. Use the Run function to start it, and Send to fill the requested log-in fields. To search for and press the "Start" button stated in the initial window of the application, use the following:

; Start Mysourcecontrol.exe
Run("C:\Prigram Files\MySourceControl\Mysourcecontrol.exe")
; Wait until the window with My Source Control title becomes active
Local $hWnd = WinWaitActive("My Source Control","")
; Write the user and password
Send("my.user")
Send("{TAB}")
Send("mypassword")
Sleep(2000)

; Define members
Local $clicked = 0
Local $buttonName = "Start"
Local $timeout = 10

; Wait recursively 10 seconds for active button
For $i = 1 To $timeout Step 1
      ; Check if the Start button is active
      If ControlCommand($hWnd, "", $buttonName, "IsEnabled", "") <> 0 Then
         ; Click the button and end the loop
         $clicked = ControlClick($hWnd, "", "[TEXT:" & $buttonName & "]")
         If $clicked = 1 Then
            ExitLoop
         EndIf
      EndIf
      Sleep(1000)
Next

How's that coffee? Good, hmm!

Bibliography and useful links

  1. https://www.future-processing.pl/blog/automation-of-desktop-applications/

  2. http://alternativeto.net/software/autoit/

  3. https://www.autoitscript.com/site/autoit/

  4. https://www.ravellosystems.com/blog/review-5-modern-test-tools-ui/

  5. https://www.autoitscript.com/autoit3/docs/intro/au3spy.htm

  6. http://autoit-script.ru/autoit\_rv\_ua/files/Other/AutoIt\_v3-\_Your\_Quick\_Guide.pdf

Conference

Sponsors

  • ntt data
  • 3PillarGlobal
  • Betfair
  • Telenav
  • Accenture
  • Siemens
  • Bosch
  • FlowTraders
  • MHP
  • Connatix
  • UIPatj
  • MetroSystems
  • Globant
  • MicroFocus
  • Colors in projects