Automated UI tests with AutoIt for QGIS

I hate testing! Yet I do understand that it is crucial for a good software solution. As I am often confronted with GUIs, I asked myself how to test them with at least manual input as possible. Here I’ll show you one way with AutoIt.

AutoIt

AutoIt editor

I stumbled upon AutoIt as it was mentioned in an article of the iX magazine where the German insurance company CosmosDirect wrote about a recent IT-project with a focus on test automation. Despite some fancy web frontends and heavy PL-SQL logic in their backend they were mentioning a fat client. Testing this fat client was not part of the story but they mantioned the tool AutoIt.

AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages.

autoitscript.com

AutoIt: a short explanation

I will not cover testing from A to Z with AutoIt. Yet I want to cover the basic ideas:

  • windows
  • shortcuts
  • passing values
  • navigation

The basic principle in this “first steps” walkthrough can be somehow described as: try to use qgis without a mouse. There are different approaches like using identifiers or “image recognition” in automated testing. But the one from AutoIt comes quite close to a plain and simply, yet powerful approach. And it is already 20 years old…

For the automated test runs AutoIt uses a specific language. Therefore you need to write test scripts. You can run them right from the editor or compile them (AutoIt runs on Windows only) and start the created exe file.

AutoIt: a quick example

In this example I use AutoIt for a smoke test after an installation on a client:

  • open latest stable executable
  • make it full screen
  • create new geopackage point layer
  • start edit session
  • create a point and add attributes
  • save layer
  • quit qgis

The script I created makes hevy use of the Send command to navigate through the GUI using TAB, DOWN_KEY and enter. I use variables for the username and the qgis window itself. The whole testrun takes only 13s to run but covers the basic functionality and can be enhanced easily with some more lines to cover the use of Grass or Python tools and some build-in functionality:

#include <AutoItConstants.au3> ; need this for the mouse clicks

Run("C:\Program Files\QGIS 3.4\bin\qgis-ltr-bin-g7.exe") ; starting the program
Local $qgis = WinWaitActive("Unbenanntes Projekt - QGIS")
WinSetState ($qgis, "", @SW_MAXIMIZE) ; maximise the working window
Send("^+n"); ctrl Shift n for new geopackage Layer
WinWaitActive("Neuer GeoPackage-Layer") ; waiting for the window to appear
Send("{TAB 3}") ; Presses the DEL key 3 times
; damn navigation fro creating a Geopackage layer...
Send("Test_Table") ;
Send("{TAB}")
Send("{DOWN}")
Send("{TAB 4}")
Send("Name") ;
Send("{TAB 2}")
Send("255")
Send("{TAB}")
Send("{SPACE}")
Send("{TAB}")
Send("C:\Users\" &amp; @UserName &amp; "\Documents\GIS DataBase\test")
Send("{ENTER}")
; should be ready now
WinWaitActive($qgis)
Send("!l") ;switch to Layer menu ALT+L
Send("{DOWN 8}")
Send("{ENTER}"); open edit mode
Send("^."); create new point
WinWaitActive($qgis)
MouseClick($MOUSE_CLICK_LEFT, 559, 411, 1) ; creates a poin on the map pane at x-y pixel 
WinWaitActive("Test_Table - Objektattribute")
Send("{TAB}"); we don't set a fid
Send("This is my name")
Send("{ENTER}"); closes attribut entry window
WinWaitActive($qgis)
Send("!l") ;switch to Layer menu ALT+L
Send("{DOWN 8}")
Send("{ENTER}"); closes edit mode
WinWaitActive("Bearbeitung beenden")
Send("{ENTER}") ;save edits
WinWaitActive($qgis)
WinClose($qgis)
WinWaitActive("Projekt speichern")
Send("{TAB 1}")
Send("{SPACE}")

Check for Result

As for every testing program: The run of this program with an exit code of 0 just says, that it ran without errors. Now we would like to check, whether or not there is a new file at the desired location. I am using a function for this:

#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

FileChecker()

Func FileChecker()
    Local $filePath = "C:\Users\" &amp; @UserName &amp; "\Documents\GIS DataBase\test.gpkg"
    Local $iFileExists = FileExists($filePath)
    ; Display a message of whether the file exists or not.
    If $iFileExists Then
        MsgBox($MB_SYSTEMMODAL, "", "The file exists." &amp; @CRLF &amp; "FileExist returned: " &amp; $iFileExists)
    Else
        MsgBox($MB_SYSTEMMODAL, "", "The file doesn't exist." &amp; @CRLF &amp; "FileExist returned: " &amp; $iFileExists)
    EndIf
    ; Delete the file:
    FileDelete($filePath)
EndFunc

Find the whole file as a gist on github.

The online documentation is very helpful and they have a forum which is very much alive. Enough Said: Happy coding!

2
Leave a Reply

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jakob Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
newest oldest most voted
Notify of
Jakob
Admin

Quite interesting software. The challenge is to prepare the test in a way all user interactions can be simulated. Often the test designer is already to deep into application logic to create all dummy situations. Thanks for sharing your thoughts about it.