Saturday, 17 June 2006

But why use AutoIt?

There are many reasons why I am writing about AutoIt and not Perl or Ruby as a mechanism for free GUI test automation on windows. Here I have listed just 5 of them, and I know that some people are going to look at the list and think that what I am touting as a good reason is a bad one for them...



Because AutoIt...

  • is well documented

  • does not require an install so I can carry it around on a usb stick

  • has a good and standard editor/IDE

  • scripts can easily be converted to .exe

  • looks and feels like basic


AutoIt is well documented

AutoIt is supplied with good documentation, in the form of a .chm file and online. There are also a lot of examples provided with the install. This makes it very easy to learn and have a good source of easy to find examples to copy. The main AutoIt website has easily accessible forums and links to plenty of 3rd party tutorials to help get you up to speed

Works from a USB stick

This means that when I install it on my system I know that I'm not going to mess up my machine installation. It also means that I have an automation platform with me wherever I go. How handy is that?

Has a good and standard editor/IDE

I do not want to count the amount of time I have spent looking for a decent portable perl IDE, or ruby IDE or C++ IDE. AutoIt picked SciTe and then configured it to act as an easy to use, code folding highlighting, code completing IDE. The tools menu when editing an au3 file makes AutoIt much much easier than it used to be.

scripts can easily be converted to .exe

This makes it a great toolsmithing platform. I can use my automation functions to create small simple utilities for automating parts of the testers job . And since AutoIt has a whole series of GUI functions I can parcel them up with a simple GUI to make life easier. I will upload a logging tool written in AutoIt later, but you can take a look at applications other people have written in AutoIt

Looks and feels like basic

Yeah I know, for most people this is not a plus point, but I have done so much basic coding in the past that I find it very easy. And I do know that this makes AutoIt very easy to pass over to people who have not got much coding experience. It also means that I do not have to worry about forgetting syntax and nuances when I have gaps between my AutoIt usage. Every time I try to use Perl GUITest I have to relearn Perl, and I know it will be good for me to get a better grasp of Perl, but sometimes I just grab the nearest thing that will work and get on with the job.

For quick and easy automation with a small learning curve and the ability to create lasting solutions - AutoIt is very useful.

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.

Monday, 12 June 2006

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

Saturday, 10 June 2006

If you install too much stuff then...

...these tools might help you out: Safarp,Startup Delayer, ShellExView



It may be obvious from the rest of the website that I install and try out a lot of different software. And yes... sometimes it doesn't work right, and I don't always install it under a virtual machine first, and my machine can get in a bit of a mess.

So there are a few tools that I use which have helped me get out of trouble in the past

Safarp is a simple little replacement for the add/remove control panel. The main benefit for me is that Safarp loads fast. In fact it loads faster than the time it takes me to even find the add/remove control panels applet. When I was having problems on one machine, Safarp was able to load the list of installed applications when the add/remove control panel applet was crashing every time I ran it. Safarp runs happily from a USB stick and so I carry it with me everywhere for those emergency uninstall moments.

Startup Delayer steps in when my machine is so clogged that I have to sit back and wait 5 minutes after boot up to get started - I guess I could uninstall stuff and figure out a way of setting up profiles so that only the apps I need are going to be running. Or I could just use R2's Startup Delayer to load the applications in the background in a time order that doesn't get in my way. I use Startup Delayer.

ShellExView helps when I have installed applications that use the context menu but don't play nice. When windows gives errors periodically after using the context menu, that often means that one of the context menu applications isn't working properly. By using ShellExView I can disable the various applications in the context menu until I work out which one is causing the problem. The good thing about being a tester is that I can, more often than not, make the periodic problems predictable and repeatable. The investigative skills I've learned when raising defects are a boon here. And that means that when I disable an application I know exactly what to do to check if the problem has gone away. ShellExView has been indispensable to me in the past, and it runs happily from a USB stick.

All of the above applications are free, so give them a try

Tuesday, 6 June 2006

Yorkspace tools list

Yorkspace.com has a small list of essential tools. I use many of these, and a few alternatives...



From the Yorkspace.com list have not used the torrent, podcast or mp3 tools, neither have I used Thunderbird.

I certainly recommend AutoIt, particularly with the SciTE4AutoIt editor. And I use VLC media player, as my default DVD viewing application.

I use the listed 7-zip and I use an alternative called PicoZip.

There are a couple of tools where I do not use the one listed, but I use something very similar and these alternatives are listed below.

I use Uniform Server instead of WAMPSERVER, but the two installs seem very similar. Take your pick and see which works best for you.

I use WinMerge and BeyondCompare instead of ExamDiff.

The Yorkspace.com list seems pretty good and if you don't have all the software listed, give some of them a try.

Sunday, 4 June 2006

Utils for a minor edge

A few useful utilities:


Switch Window provides a handy little popup window that lists all current windows and you can easily switch between them. I tend to use the [Start] context menu to trigger it.

Microsoft XP PowerToys has a few tools that I use regularly, I'm just going to draw your attention to 2 of them below

I have the Alt-Tab Replacement installed so that when I do use alt+tab, I get a thumbnail of the open window, which helps me identify exactly which window I'm looking for. This is also a great tool for testing people's powers of observation and seeing if they notice that your alt+tab is slightly more informative than theirs.

Command Prompt Here provides an extra context menu entry to open a command prompt set to the directory that you right clicked from in Explorer.

These tools give me a minor edge when I'm navigating Windows during the testing of Windows applications.