Skip to main content

Jun 13, 2006 - 5 minute read - AutoIt CompendiumDev

Simple Monkey Tester Annotated

I’m annotating the mokey tester code that I posted yesterday with even more comments for those people that are new to AutoIt and perhaps didn’t understand what the code was doing.

The monkey tester code explained.

A comment in AutoIT starts with a semi colon and runs to the end of a line. You can also comment a block of code by using the #comments-start and #comments-end directives.

; monkey tester
; This monkey tester just randomly
; clicks on the window of the calculator

Now we create a few variables for the script . Variables are of type Variant so they can hold any value and they do not have to be declared before use. $parentWindowName is the text that will be searched for in the window name. i.e. when you start up Calculator then the Title of the window is “Calculator”. If you start Notepad then the title of the window is “Untitled - Notepad”.

; the title of the window
; the name of the application to run

Run is an AutoIt command to ‘run’ an application. Normally you would have to put the full path of the application in the argument e.g. “c:appsmyapp.exe” but some applications like “calc” and “notepad” can be run directly.

; run the application

WinWaitActive is a command that you will probably end up using a lot in your scripts. It basically does what it says and pauses the script (waits) until a window is active. It tells if the window is active by looking for the value of $parentWindowName in the window title. The function WinWaitActive will return 0 if the window could not be found within the timeout period (in seconds, in this case defined as 3).

; wait until the window is showing, time out in 3 secs

This is the only really complicated part of the script. I get the Handle of the window using the window title. The Handle is the id that Windows recognises the window with and I’m using it because WinActive can be quite slow to find the window by using the title as it has to cycle around all the windows in the system matching the text. But by using the handle later on, all it has to match is the id and it can find this pretty quickly.

; get the handle of the window

We create a for loop to decide how long to monkey around for. AutoIt has several loop constructs: For…Next , While…WEnd, Do…Until

; monkey around 100 times
for $x = 1 to 100

I use the WinGetHandle function here to check if the same window is still present as another window with the same title may have appeared and we may have accidentally switched to it. Synchronisation is a key part of automated testing when using tools like AutoIt and much of your automation code is going to be code to check if the preconditions for the action you are about to do are still true, and if not then either stop the script or do somthing to make them true. In the case of this script we are just going to stop if we cannot find the window we started with. AutoIt has a lot of GUI commands for interacting with the user but here we are using a simple MessageBox dialog by using the MsgBox function.

; check if the window is still available and if so activate it
if WinGetHandle($parentWindowName)<>$mainHwnd Then
   ; we lost the app
   MsgBox(1,"App Gone","App is lost")

The WinGetPos function returns an array. the array returned has 4 entries and is numbered starting at 0 so $array[0] ->

; get the position of the window
; - do it each time in the loop
; just in case it moved, or changed size

So now we are going to generate 2 random numbers, one for the x value so between the x and x+width values. And one for the y value which is between y and y+height. This should give us a position inside the window.

; choose an x y value in the window
; (it might hit the close button)

We take the random values that we generated , move the mouse to the position and issue a “left” click of the mouse.

; move to the position and click it

Then we loop back around until we have done 100 monkey actions.


Now that was overly explained for the short example but since it is the first script I’ve shown you I thought it best to go through it in some detail.

The AutoIt help file is actually pretty good and if you look up each of the commands in the help file then you will learn a lot more than I have written up here.

The AutoIt language is pretty close to a dialect of basic which should make it pretty simple for anyone to pick up. One of the hardest things, as your scripts get larger are the lack of data structures for variables. You can get around that by using AutoIt in its dll form and using your favourite progamming language instead of AutoIt. But there are reasons why I like the AutoIt language itself and…I’ll cover those later.

- in AutoIt CompendiumDev

You will need a Github account to comment. Or you can contact me with your comment.

I reserve the right to delete spam comments e.g. if your comment adds no value and its purpose is simply to create a backlink to another site offering training, or courses, or etc.