How to write a simple Monkey Tester in AutoIt
AutoIt is a simple windows automation language, and in this short code snippet I will give you the code for a very simple Monkey tester.
I'm tempted to talk a little bit about what AutoIt is, and what it does, but I'll save that for a later post. Right now I just want to dive into the action and show you a simple script that does a little monkey testing.
In this example we are using the built in Calculator for windows and all we are doing is randomly clicking on the application. It seems unlikely that this will expose any errors in Calculator but this code is here to demonstrate the principle and show how simple it all is.
In order to use the code you are going to have to go off to the AutoIt web site and download AutoIt v3 and I also recommend that you download the SciTE AutoIt Editor
- Install the applications,
- run SciTE,
- paste the code below into the editor,
- press F5 to run the code,
- watch the monkey tester go to work.
See what you can learn from the code itself and by reading the AutoIt help files. It is extensively commented for such a little application.
; monkey tester
; This monkey tester just randomly
; clicks on the window of the calculator
; the title of the window
$parentWindowName = "Calculator"
; the name of the application to run
$programRunName = "calc"
; run the application
run($programRunName)
; wait until the window is showing, time out in 3 secs
WinWaitActive($parentWindowName,"",3)
; get the handle of the window
$mainHwnd = WinGetHandle($parentWindowName)
; monkey around 100 times
for $x=1 to 100
; 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")
Exit
Else
winActivate ($mainHwnd)
EndIf
; get the position of the window
; - do it each time in the loop
; just in case it moved, or changed size
$pos = WinGetPos($mainHwnd)
; choose an x y value in the window
; (it might hit the close button)
$rndX = random($pos[0],$pos[0]+$pos[2])
$rndY = random($pos[1],$pos[1]+$pos[3])
; move to the position and click it
mousemove ($rndX,$rndY,0)
mouseclick("left")
next

What is the point of this script?
Certainly, if you run it long enough, the Calculator will be “gone”. But you probably won’t have found a bug. Nor will you know what you did to make the Calculator go away.
Similarly, if the application crashes, you may have found a bug, but will not know why it crashed, right?
Hi Joe, Thanks for leaving the comment.
I intended this script to show that you can write a simple monkey test automation script in AutoIt.
I don’t class it as a ‘good’ monkey tester - after all it has a fixed 100 iteration loop, no logging and no ability to recognise any application error conditions other than ‘gone’ - but you can change all that quite easily if you wanted to use this type of approach.
In order to use this type of test approach on a real project you would have to add additional code to add logging so you know what actions were taken.
I have other versions of this script that I wrote when learning more about AutoIt that I have not posted - these slowly become more ‘intelligent’ in that they don’t randomly click on the app, they randomly trigger app specific events e.g. click on ‘1′, click on ‘+’ then any logging makes more sense than “clicked at pos 100, 200″.
Monkey Testing may not add any value to a testing project.
Monkey Testing generally has such a low overhead in terms of initial investment that it may prove worth experimenting with.
Caveats for ‘Monkey’ testing:
In order to know ‘why’ it crashed I would expect to have to do additional investigation to identify some plausible reasons (logging would help me with this) - I would not want to rely on a Monkey to do this for me.
Hope that helps.
Alan