Tuesday, 13 June 2006

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
$parentWindowName="Calculator"
; the name of the application to run
$programRunName="calc"

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:\apps\myapp.exe" but some applications like "calc" and "notepad" can be run directly.

; run the application
run($programRunName)

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
WinWaitActive($parentWindowName,"",3)

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
$mainHwnd=WinGetHandle($parentWindowName)

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=1to100

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
ifWinGetHandle($parentWindowName)<>$mainHwndThen
; we lost the app
MsgBox(1,"App Gone","App is lost")
Exit
Else

winActivate($mainHwnd)
EndIf

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

$pos=WinGetPos($mainHwnd)

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)

$rndX=random($pos[0],$pos[0]+$pos[2])
$rndY=random($pos[1],$pos[1]+$pos[3])

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
mousemove($rndX,$rndY,0)
mouseclick("left")

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

next

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.

No comments:

Post a Comment